Python Tutorial
Python tutorial provides basic and advanced concepts of Python. Our Python tutorial is designed for beginners and professionals. Python is a simple, general purpose, high level, and object-oriented programming language. Python is an interpreted scripting language also. Guido Van Rossum is known as the founder of Python programming. Our Python tutorial includes all topics of Python Programming such as installation, control statements, Strings, Lists, Tuples, Dictionary, Modules, Exceptions, Date and Time, File I/O, Programs, etc. There are also given Python interview questions to help you better understand Python Programming. What is PythonPython is a general purpose, dynamic, high-level, and interpreted programming language. It supports Object Oriented programming approach to develop applications. It is simple and easy to learn and provides lots of high-level data structures. Python is easy to learn yet powerful and versatile scripting language, which makes it attractive for Application Development. Python's syntax and dynamic typing with its interpreted nature make it an ideal language for scripting and rapid application development. Python supports multiple programming pattern, including object-oriented, imperative, and functional or procedural programming styles. Python is not intended to work in a particular area, such as web programming. That is why it is known as multipurpose programming language because it can be used with web, enterprise, 3D CAD, etc. We don't need to use data types to declare variable because it is dynamically typed so we can write a=10 to assign an integer value in an integer variable. Python makes the development and debugging fast because there is no compilation step included in Python development, and edit-test-debug cycle is very fast. Python 2 vs. Python 3In most of the programming languages, whenever a new version releases, it supports the features and syntax of the existing version of the language, therefore, it is easier for the projects to switch in the newer version. However, in the case of Python, the two versions Python 2 and Python 3 are very much different from each other. A list of differences between Python 2 and Python 3 are given below:
Python HistoryPython was invented by Guido van Rossum in 1991 at CWI in Netherland. The idea of Python programming language has taken from the ABC programming language or we can say that ABC is a predecessor of Python language. There is also a fact behind the choosing name Python. Guido van Rossum was a fan of the popular BBC comedy show of that time, "Monty Python's Flying Circus". So he decided to pick the name Python for his newly created programming language. Python has the vast community across the world and releases its version within the short period. Why learn Python?Python provides many useful features to the programmer. These features make it most popular and widely used language. We have listed below few-essential feature of Python.
Where is Python used?Python is a general-purpose, popular programming language and it is used in almost every technical field. The various areas of Python use are given below.
Python Basic SyntaxThere is no use of curly braces or semicolon in Python programming language. It is English-like language. But Python uses the indentation to define a block of code. Indentation is nothing but adding whitespace before the statement when it is needed. For example -
In the above example, the statements that are same level to right belong to the function. Generally, we can use four whitespaces to define indentation. Python First ProgramUnlike the other programming languages, Python provides the facility to execute the code using few lines. For example - Suppose we want to print the "Hello World" program in Java; it will take three lines to print it.
On the other hand, we can do this using one statement in Python.
Both programs will print the same result, but it takes only one statement without using a semicolon or curly braces in Python. Python Popular Frameworks and LibrariesPython has wide range of libraries and frameworks widely used in various fields such as machine learning, artificial intelligence, web applications, etc. We define some popular frameworks and libraries of Python as follows.
Python print() FunctionThe print() function displays the given object to the standard output device (screen) or to the text stream file. Unlike the other programming languages, Python print() function is most unique and versatile function. The syntax of print() function is given below.
Let's explain its parameters one by one.
Let's understand the following example. Example - 1: Return a value
Output: Welcome to javaTpoint. a = 10 a = 10 = b As we can see in the above output, the multiple objects can be printed in the single print() statement. We just need to use comma (,) to separate with each other. Example - 2: Using sep and end argument
Output: a =dddd10 a =010$$$$$ In the first print() statement, we use the sep and end arguments. The given object is printed just after the sep values. The value of end parameter printed at the last of given object. As we can see that, the second print() function printed the result after the three black lines. Taking Input to the UserPython provides the input() function which is used to take input from the user. Let's understand the following example. Example -
Output: Enter a name of student: Devansh The student name is: Devansh By default, the input() function takes the string input but what if we want to take other data types as an input. If we want to take input as an integer number, we need to typecast the input() function into an integer. For example - Example -
Output: Enter first number: 50 Enter second number: 100 150 We can take any type of values using input() function. Python OperatorsOperators are the symbols which perform various operations on Python objects. Python operators are the most essential to work with the Python data types. In addition, Python also provides identify membership and bitwise operators. We will learn all these operators with the suitable example in following tutorial.
Python Conditional StatementsConditional statements help us to execute a particular block for a particular condition. In this tutorial, we will learn how to use the conditional expression to execute a different block of statements. Python provides if and else keywords to set up logical conditions. The elif keyword is also used as conditional statement.
Python Loops
Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times. For this purpose, the programming languages provide various types of loops capable of repeating some specific code several times. Consider the following tutorial to understand the statements in detail.
Python Data StructuresData structures are referred which can hold some data together or we say that they are used to store the data in organized way. Python provides built-in data structures such as list, tuple, dictionary, and set. We can perform complex tasks using data structures. Python ListPython list holds the ordered collection of items. We can store a sequence of items in a list. Python list is mutable which means it can be modified after its creation. The items of lists are enclosed within the square bracket [] and separated by the comma. Let's see the example of list.
If we try to print the type of L1, L2, and L3 using type() function then it will come out to be a list.
Output: <class 'list'> <class 'list'> To learn more about list, visit the following tutorial.
Python TuplePython Tuple is used to store the sequence of immutable Python objects. The tuple is similar to lists since the value of the items stored in the list can be changed, whereas the tuple is immutable, and the value of the items stored in the tuple cannot be changed. Tuple can be defined as follows Example -
Output: <class 'tuple'>
('Apple', 'Mango', 'Orange', 'Banana')
If we try to add new to the tuple, it will throw an error. Example -
Output: Traceback (most recent call last): File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/gamewithturtle.py", line 3, in The above program throws an error because tuples are immutable type. To learn more about tuple, visit the Python Tuples.
Python StringPython string is a sequence of characters. It is a collection of the characters surrounded by single quotes, double quotes, or triple quotes. It can also define as collection of the Unicode characters. We can create a string as follows. Example -
Output: Hi Python Hi Python Hi Python Python doesn't support the character data-type. A single character written as 'p' is treated as a string of length 1. Stings are also immutable. We can't change after it is declared. To learn more about the string, visit the following tutorial.
DictionariesPython Dictionary is a most efficient data structure and used to store the large amount of data. It stores the data in the key-value pair format. Each value is stored corresponding to its key. Keys must be a unique and value can be any type such as integer, list, tuple, etc. It is a mutable type; we can reassign after its creation. Below is the example of creating dictionary in Python. Example -
Output: <class 'dict'>
Printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 250000, 'Company': 'GOOGLE'}
The empty curly braces {} are used to create empty dictionary. To learn more, visit the complete tutorial of the dictionary.
Python SetsA Python set is a collection of unordered elements. Each element in set must be unique and immutable. Sets are mutable which means we can modify anytime throughout the program. Let's understand the example of creating set in Python. Example -
Output: {'March', 'July', 'April', 'May', 'June', 'February', 'January'}
<class 'set'>
To get the more information about sets, visit the following resources.
Python Functional ProgrammingThis section of Python tutorial defines some important tools related to functional programming such as lambda and recursive functions. These functions are very efficient in accomplishing the complex tasks. We define a few important functions, such as reduce, map, and filter. Python provides the functools module that includes various functional programming tools. Visit the following tutorial to learn more about functional programming.
Python File I/OFiles are used to store data in a computer disk. In this tutorial, we explain the built-in file object of Python. We can open a file using Python script and perform various operations such as writing, reading, and appending. There are various ways of opening a file. We are explained with the relevant example. We will also learn to perform read/write operations on binary files.
Python ModulesPython modules are the program files that contain a Python code or functions. There are two types of module in the Python - User-define modules and built-in modules. A module that the user defines, or we can say that our Python code saved with .py extension, is treated as a user-define module. Built-in modules are predefined modules of Python. To use the functionality of the modules, we need to import them into our current working program.
Python ExceptionsAn exception can be defined as an unusual condition in a program resulting in the interruption in the flow of the program. Whenever an exception occurs, the program stops the execution, and thus the further code is not executed. Therefore, an exception is the run-time errors that are unable to handle to Python script. An exception is a Python object that represents an error.
Python CSVA csv stands for "comma separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a common format for data interchange. A csv file opens into the excel sheet, and the rows and columns data define the standard format. Visit the following tutorial to learn the CSV module in detail.
Python Sending MailWe can send or read a mail using the Python script. Python's standard library modules are useful for handling various protocols such as PoP3 and IMAP. We will learn how to send a mail with the popular email service SMTP from a Python script.
Python Magic MethodsPython magic method is defined as the special method which adds "magic" to a class. It starts and ends with double underscores, for example, _init_ or _str_. The built-in classes define many magic methods. The dir() function can be used to see the number of magic methods inherited by a class. It has two prefixes and suffix underscores in the method name.
Python Oops ConceptsEverything in Python is treated as an object including integer values, floats, functions, classes, and none. Apart from that, Python supports all oriented concepts. Below is the brief introduction of oops concepts of Python.
To read the oops concept in detail, visit the following resources.
Python Advance TopicsPython includes many advance and useful concepts that help the programmer to solve the complex tasks. These concepts are given below. Python IteratorAn iterator is simply an object that can be iterated upon. It returns one object at a time. It can be implemented using the two special methods, __iter__() and __next__(). To learn more about the iterators visit our Python Iterators tutorial. Python GeneratorsThe Generators are an easiest way of creating Iterators. To learn more about, visit our Python Generators tutorial. Python DecoratorsThese are used to modify the behavior of the function. Decorators provide the flexibility to wrap another function to expand the working of wrapped function, without permanently modifying it. To learn more about, visit the Python Decorators tutorial. Python Database ConnectionsWe can use various databases along with Python. You can learn the full tutorial to visit below resources. Python DBI-API acclaims standard sets of functionality to be included in the database connectivity modules for respective RDBMS products. We explain all important database connectivity using Python DBI-API. Python MySQLEnvironment Setup Database Connection Creating New Database Creating Tables Insert Operation Read Operation Update Operation Join Operation Performing Transactions Python MongoDBPython MongoDB Python SQLitePython SQLite Python CGIPython CGI stands for "Common Gateway Interface", which is used to define how to exchange information between the webserver and a custom Python scripts. The Common Gateway Interface is a standard for external gateway programs to interface with the server, such as HTTP Servers. To learn more about Python CGI, visit the following tutorial.
PrerequisiteBefore learning Python, you must have the basic knowledge of programming concepts. AudienceOur Python tutorial is designed to help beginners and professionals. ProblemWe assure that you will not find any problem in this Python tutorial. But if there is any mistake, please post the problem in contact form.
Next TopicFeatures of Python
|
Python FeaturesPython provides many useful features which make it popular and valuable from the other programming languages. It supports object-oriented programming, procedural programming approaches and provides dynamic memory allocation. We have listed below a few essential features. 1) Easy to Learn and UsePython is easy to learn as compared to other programming languages. Its syntax is straightforward and much the same as the English language. There is no use of the semicolon or curly-bracket, the indentation defines the code block. It is the recommended programming language for beginners. 2) Expressive LanguagePython can perform complex tasks using a few lines of code. A simple example, the hello world program you simply type print("Hello World"). It will take only one line to execute, while Java or C takes multiple lines. 3) Interpreted LanguagePython is an interpreted language; it means the Python program is executed one line at a time. The advantage of being interpreted language, it makes debugging easy and portable. 4) Cross-platform LanguagePython can run equally on different platforms such as Windows, Linux, UNIX, and Macintosh, etc. So, we can say that Python is a portable language. It enables programmers to develop the software for several competing platforms by writing a program only once. 5) Free and Open SourcePython is freely available for everyone. It is freely available on its official website www.python.org. It has a large community across the world that is dedicatedly working towards make new python modules and functions. Anyone can contribute to the Python community. The open-source means, "Anyone can download its source code without paying any penny." 6) Object-Oriented LanguagePython supports object-oriented language and concepts of classes and objects come into existence. It supports inheritance, polymorphism, and encapsulation, etc. The object-oriented procedure helps to programmer to write reusable code and develop applications in less code. 7) ExtensibleIt implies that other languages such as C/C++ can be used to compile the code and thus it can be used further in our Python code. It converts the program into byte code, and any platform can use that byte code. 8) Large Standard LibraryIt provides a vast range of libraries for the various fields such as machine learning, web developer, and also for the scripting. There are various machine learning libraries, such as Tensor flow, Pandas, Numpy, Keras, and Pytorch, etc. Django, flask, pyramids are the popular framework for Python web development. 9) GUI Programming SupportGraphical User Interface is used for the developing Desktop application. PyQT5, Tkinter, Kivy are the libraries which are used for developing the web application. 10) IntegratedIt can be easily integrated with languages like C, C++, and JAVA, etc. Python runs code line by line like C,C++ Java. It makes easy to debug the code. 11. EmbeddableThe code of the other programming language can use in the Python source code. We can use Python source code in another programming language as well. It can embed other language into our code. 12. Dynamic Memory AllocationIn Python, we don't need to specify the data-type of the variable. When we assign some value to the variable, it automatically allocates the memory to the variable at run time. Suppose we are assigned integer value 15 to x, then we don't need to write int x = 15. Just write x = 15.
Next TopicPython History
|
Python History and Versions
Why the Name Python?There is a fact behind choosing the name Python. Guido van Rossum was reading the script of a popular BBC comedy series "Monty Python's Flying Circus". It was late on-air 1970s. Van Rossum wanted to select a name which unique, sort, and little-bit mysterious. So he decided to select naming Python after the "Monty Python's Flying Circus" for their newly created programming language. The comedy series was creative and well random. It talks about everything. Thus it is slow and unpredictable, which made it very interesting. Python is also versatile and widely used in every technical field, such as Machine Learning, Artificial Intelligence, Web Development, Mobile Application, Desktop Application, Scientific Calculation, etc. Python Version ListPython programming language is being updated regularly with new features and supports. There are lots of update in Python versions, started from 1994 to current release. A list of Python versions with its released date is given below.
Tips to Keep in Mind While Learning PythonThe most common question asked by the beginners - "What is the best way to learn Python"? It is the initial and relevant question because first step in learning any programming language is to know how to learn. The proper way of learning will help us to learn fast and become a good Python developer. In this section, we will discuss various tips that we should keep in mind while learning Python. 1. Make it Clear Why We Want to LearnThe goal should be clear before learning the Python. Python is an easy, a vast language as well. It includes numbers of libraries, modules, in-built functions and data structures. If the goal is unclear then it will be a boring and monotonous journey of learning Python. Without any clear goal, you perhaps won't make it done. So, first figure out the motivation behind learning, which can anything be such as knowing something new, develop projects using Python, switch to Python, etc. Below are the general areas where Python is widely used. Pick any of them.
Choose any one or two areas according to your interest and start the journey towards learning Python. 2. Learn the Basic SyntaxIt is the most essential and basic step to learn the syntax of the Python programming language. We have to learn the basic syntax before dive deeper into learning it. As we have discussed in our earlier tutorial, Python is easy to learn and has a simple syntax. It doesn't use semicolon and brackets. Its syntax is like the English language. So it will take minimum amount of time to learning its syntax. Once we get its syntax properly, further learning will be easier and quicker getting to work on projects. Note - Learn Python 3, not Python 2.7, because the industry no longer uses it. Our Python tutorial is based on its latest version Python 3.3. Write Code by OwnWriting the code is the most effective and robust way to learn Python. First, try to write code on paper and run in mind (Dry Run) then move to the system. Writing code on paper will help us get familiar quickly with the syntax and the concept store in the deep memory. While writing the code, try to use proper functions and suitable variables names. There are many editors available for Python programming which highlights the syntax related issue automatically. So we don't need to pay lot of attention of these mistakes. 4. Keep PracticingThe next important step is to do the practice. It needs to implementing the Python concepts through the code. We should be consistence to our daily coding practice. Consistency is the key of success in any aspect of life not only in programming. Writing code daily will help to develop muscle memory. We can do the problem exercise of related concepts or solve at least 2 or 3 problems of Python. It may seem hard but muscle memory plays large part in programing. It will take us ahead from those who believe only the reading concept of Python is sufficient. 5. Make Notes as NeededCreating notes by own is an excellent method to learn the concepts and syntax of Python. It will establish stability and focus that helps you become a Python developer. Make brief and concise notes with relevant information and include appropriate examples of the subject concerned. Maintain own notes are also helped to learn fast. A study published in Psychological Science that - The students who were taking longhand notes in the studies were forced to be more selective — because you can't write as fast as you can type. 6. Discuss Concepts with OtherCoding seems to be solitary activity, but we can enhance our skills by interacting with the others. We should discuss our doubts to the expert or friends who are learning Python. This habit will help to get additional information, tips and tricks, and solution of coding problems. One of the best advantages of Python, it has a great community. Therefore, we can also learn from passionate Python enthusiasts. 7. Do small ProjectsAfter understanding Python's basic concept, a beginner should try to work on small projects. It will help to understand Python more deeply and become more component in it. Theoretical knowledge is not enough to get command over the Python language. These projects can be anything as long as they teach you something. You can start with the small projects such as calculator app, a tic-toc-toe game, an alarm clock app, a to-do list, student or customer management system, etc. Once you get handy with a small project, you can easily shift toward your interesting domain (Machine Learning, Web Development, etc.). 8. Teach OthersThere is a famous saying that "If you want to learn something then you should teach other". It is also true in case of learning Python. Share your information to other students via creating blog posts, recording videos or taking classes in local training center. It will help us to enhance the understanding of Python and explore the unseen loopholes in your knowledge. If you don't want to do all these, join the online forum and post your answers on Python related questions. 9. Explore Libraries and FrameworksPython consists of vast libraries and various frameworks. After getting familiar with Python's basic concepts, the next step is to explore the Python libraries. Libraries are essential to work with the domain specific projects. In the following section, we describe the brief introduction of the main libraries.
There are many libraries in Python. Above, we have mentioned a few of them. 10. Contribute to Open SourceAs we know, Python is an open source language that means it is freely available for everyone. We can also contribute to Python online community to enhance our knowledge. Contributing to open source projects is the best way to explore own knowledge. We also receive the feedback, comments or suggestions for work that we submitted. The feedback will enable the best practices for Python programming and help us to become a good Python developer. Usage of PythonPython is a general purpose, open source, high-level programming language and also provides number of libraries and frameworks. Python has gained popularity because of its simplicity, easy syntax and user-friendly environment. The usage of Python as follows.
In the next topic, we will discuss the Python Application, where we have defined Python's usage in detail.
Next TopicPython Applications
|
Python ApplicationsPython is known for its general-purpose nature that makes it applicable in almost every domain of software development. Python makes its presence in every emerging field. It is the fastest-growing programming language and can develop any application. Here, we are specifying application areas where Python can be applied.
1) Web ApplicationsWe can use Python to develop web applications. It provides libraries to handle internet protocols such as HTML and XML, JSON, Email processing, request, beautifulSoup, Feedparser, etc. One of Python web-framework named Django is used on Instagram. Python provides many useful frameworks, and these are given below:
2) Desktop GUI ApplicationsThe GUI stands for the Graphical User Interface, which provides a smooth interaction to any application. Python provides a Tk GUI library to develop a user interface. Some popular GUI libraries are given below.
3) Console-based ApplicationConsole-based applications run from the command-line or shell. These applications are computer program which are used commands to execute. This kind of application was more popular in the old generation of computers. Python can develop this kind of application very effectively. It is famous for having REPL, which means the Read-Eval-Print Loop that makes it the most suitable language for the command-line applications. Python provides many free library or module which helps to build the command-line apps. The necessary IO libraries are used to read and write. It helps to parse argument and create console help text out-of-the-box. There are also advance libraries that can develop independent console apps. 4) Software DevelopmentPython is useful for the software development process. It works as a support language and can be used to build control and management, testing, etc.
5) Scientific and NumericThis is the era of Artificial intelligence where the machine can perform the task the same as the human. Python language is the most suitable language for Artificial intelligence or machine learning. It consists of many scientific and mathematical libraries, which makes easy to solve complex calculations. Implementing machine learning algorithms require complex mathematical calculation. Python has many libraries for scientific and numeric such as Numpy, Pandas, Scipy, Scikit-learn, etc. If you have some basic knowledge of Python, you need to import libraries on the top of the code. Few popular frameworks of machine libraries are given below.
6) Business ApplicationsBusiness Applications differ from standard applications. E-commerce and ERP are an example of a business application. This kind of application requires extensively, scalability and readability, and Python provides all these features. Oddo is an example of the all-in-one Python-based application which offers a range of business applications. Python provides a Tryton platform which is used to develop the business application. 7) Audio or Video-based ApplicationsPython is flexible to perform multiple tasks and can be used to create multimedia applications. Some multimedia applications which are made by using Python are TimPlayer, cplay, etc. The few multimedia libraries are given below.
8) 3D CAD ApplicationsThe CAD (Computer-aided design) is used to design engineering related architecture. It is used to develop the 3D representation of a part of a system. Python can create a 3D CAD application by using the following functionalities.
9) Enterprise ApplicationsPython can be used to create applications that can be used within an Enterprise or an Organization. Some real-time applications are OpenERP, Tryton, Picalo, etc. 10) Image Processing ApplicationPython contains many libraries that are used to work with the image. The image can be manipulated according to our requirements. Some libraries of image processing are given below.
In this topic, we have described all types of applications where Python plays an essential role in the development of these applications. In the next tutorial, we will learn more concepts about Python.
Next TopicHow To Install Python
|
How to Install Python (Environment Set-up)In order to become Python developer, the first step is to learn how to install or update Python on a local machine or computer. In this tutorial, we will discuss the installation of Python on various operating systems. Installation on WindowsVisit the link https://www.python.org/downloads/ to download the latest release of Python. In this process, we will install Python 3.8.6 on our Windows operating system. When we click on the above link, it will bring us the following page. Step - 1: Select the Python's version to download. Click on the download button.
Step - 2: Click on the Install Now Double-click the executable file, which is downloaded; the following window will open. Select Customize installation and proceed. Click on the Add Path check box, it will set the Python path automatically.
We can also click on the customize installation to choose desired location and features. Other important thing is install launcher for the all user must be checked. Step - 3 Installation in Process
Now, try to run python on the command prompt. Type the command python -version in case of python3.
We are ready to work with the Python. Installation on MacTo install python3 on MacOS, visit the link https://www.javatpoint.com/how-to-install-python-on-mac and follow the instructions given in the tutorial. Installation on CentOSTo install Python3 on CentOS, visit the link https://www.javatpoint.com/how-to-install-python-on-centos and follow the instructions given in the tutorial. Installation on UbuntuTo install Python3 on Ubuntu, visit the link https://www.javatpoint.com/how-to-install-python-in-ubuntu and follow the instructions given in the tutorial.
Next TopicFirst Python Program
|
First Python ProgramIn this Section, we will discuss the basic syntax of Python, we will run a simple program to print Hello World on the console. Python provides us the two ways to run a program:
Let's discuss each one of them in detail. Interactive interpreter promptPython provides us the feature to execute the Python statement one by one at the interactive prompt. It is preferable in the case where we are concerned about the output of each line of our Python program. To open the interactive mode, open the terminal (or command prompt) and type python (python3 in case if you have Python2 and Python3 both installed on your system). It will open the following prompt where we can execute the Python statement and check their impact on the console.
After writing the print statement, press the Enter key.
Here, we get the message "Hello World !" printed on the console. Using a script file (Script Mode Programming)The interpreter prompt is best to run the single-line statements of the code. However, we cannot write the code every-time on the terminal. It is not suitable to write multiple lines of code. Using the script mode, we can write multiple lines code into a file which can be executed later. For this purpose, we need to open an editor like notepad, create a file named and save it with .py extension, which stands for "Python". Now, we will implement the above example using the script mode.
To run this file named as first.py, we need to run the following command on the terminal.
Step - 1: Open the Python interactive shell, and click "File" then choose "New", it will open a new blank script in which we can write our code.
Step -2: Now, write the code and press "Ctrl+S" to save the file.
Step - 3: After saving the code, we can run it by clicking "Run" or "Run Module". It will display the output to the shell.
The output will be shown as follows.
Step - 4: Apart from that, we can also run the file using the operating system terminal. But, we should be aware of the path of the directory where we have saved our file.
Multi-line StatementsMulti-line statements are written into the notepad like an editor and saved it with .py extension. In the following example, we have defined the execution of the multiple code lines using the Python script. Code:
Script File:
Pros and Cons of Script ModeThe script mode has few advantages and disadvantages as well. Let's understand the following advantages of running code in script mode.
Let's see the disadvantages of the script mode.
Get Started with PyCharmIn our first program, we have used gedit on our CentOS as an editor. On Windows, we have an alternative like notepad or notepad++ to edit the code. However, these editors are not used as IDE for python since they are unable to show the syntax related suggestions. JetBrains provides the most popular and a widely used cross-platform IDE PyCharm to run the python programs. PyCharm installationAs we have already stated, PyCharm is a cross-platform IDE, and hence it can be installed on a variety of the operating systems. In this section of the tutorial, we will cover the installation process of PyCharm on Windows, MacOS, CentOS, and Ubuntu. WindowsInstalling PyCharm on Windows is very simple. To install PyCharm on Windows operating system, visit the link https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows to download the executable installer. Double click the installer (.exe) file and install PyCharm by clicking next at each step. To create a first program to Pycharm follows the following step. Step - 1. Open Pycharm editor. Click on "Create New Project" option to create new project.
Step - 2. Select a location to save the project.
Step - 3. Click on "File" menu and select "New". By clicking "New" option it will show various file formats. Select the "Python File".
Step - 4. Now type the name of the Python file and click on "OK". We have written the "FirstProgram".
Step - 5. Now type the first program - print("Hello World") then click on the "Run" menu to run program.
Step - 6. The output will appear at the bottom of the screen.
Basic Syntax of PythonIndentation and Comment in PythonIndentation is the most significant concept of the Python programming language. Improper use of indentation will end up "IndentationError" in our code. Indentation is nothing but adding whitespaces before the statement when it is needed. Without indentation Python doesn't know which statement to be executed to next. Indentation also defines which statements belong to which block. If there is no indentation or improper indentation, it will display "IndentationError" and interrupt our code.
Python indentation defines the particular group of statements belongs to the particular block. The programming languages such as C, C++, java use the curly braces {} to define code blocks. In Python, statements that are the same level to the right belong to the same block. We can use four whitespaces to define indentation. Let's see the following lines of code. Example -
Output: 1 2 3 4 End of for loop Explanation: In the above code, for loop has a code blocks and if the statement has its code block inside for loop. Both indented with four whitespaces. The last print() statement is not indented; that's means it doesn't belong to for loop. Comments in PythonComments are essential for defining the code and help us and other to understand the code. By looking the comment, we can easily understand the intention of every line that we have written in code. We can also find the error very easily, fix them, and use in other applications. In Python, we can apply comments using the # hash character. The Python interpreter entirely ignores the lines followed by a hash character. A good programmer always uses the comments to make code under stable. Let's see the following example of a comment.
We can add comment in each line of the Python code.
It is good idea to add code in any line of the code section of code whose purpose is not obvious. This is a best practice to learn while doing the coding. Types of CommentPython provides the facility to write comments in two ways- single line comment and multi-line comment. Single-Line Comment - Single-Line comment starts with the hash # character followed by text for further explanation.
We can also write a comment next to a code statement. Consider the following example.
Multi-Line Comments - Python doesn't have explicit support for multi-line comments but we can use hash # character to the multiple lines. For example -
We can also use another way.
This is the basic introduction of the comments. Visit our Python Comment tutorial to learn it in detail. Python IdentifiersPython identifiers refer to a name used to identify a variable, function, module, class, module or other objects. There are few rules to follow while naming the Python Variable.
Let's understand the following example. Example -
Output: 10 100 1000 We have defined the basic syntax of the Python programming language. We must be familiar with the core concept of any programming languages. Once we memorize the concepts as mentioned above. The journey of learning Python will become easier. CentOSTo install PyCharm on CentOS, visit the link https://www.javatpoint.com/how-to-install-pycharm-on-centos. The link will guide you to install PyCharm on the CentOS. MacOSTo install PyCharm on MacOS, visit the link https://www.javatpoint.com/how-to-install-pycharm-on-mac. The link will guide you to install PyCharm on the MacOS. UbuntuTo install PyCharm on Ubuntu, visit the link https://www.javatpoint.com/how-to-install-pycharm-in-ubuntu. The link will guide you to install PyCharm on Ubuntu.
Next TopicPython Variables
|
Python VariablesVariable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value. In Python, we don't need to specify the type of variable because Python is a infer language and smart enough to get variable type. Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore. It is recommended to use lowercase letters for the variable name. Rahul and rahul both are two different variables. Identifier NamingVariables are the example of identifiers. An Identifier is used to identify the literals used in the program. The rules to name an identifier are given below.
Declaring Variable and Assigning ValuesPython does not bind us to declare a variable before using it in the application. It allows us to create a variable at the required time. We don't need to declare explicitly variable in Python. When we assign any value to the variable, that variable is declared automatically. The equal (=) operator is used to assign value to a variable. Object ReferencesIt is necessary to understand how the Python interpreter works when we declare a variable. The process of treating variables is somewhat different from many other programming languages. Python is the highly object-oriented programming language; that's why every data item belongs to a specific type of class. Consider the following example.
Output: John The Python object creates an integer object and displays it to the console. In the above print statement, we have created a string object. Let's check the type of it using the Python built-in type() function.
Output: <class 'str'> In Python, variables are a symbolic name that is a reference or pointer to an object. The variables are used to denote objects by that name. Let's understand the following example
In the above image, the variable a refers to an integer object. Suppose we assign the integer value 50 to a new variable b. a = 50 b = a
The variable b refers to the same object that a points to because Python does not create another object. Let's assign the new value to b. Now both variables will refer to the different objects. a = 50 b =100
Python manages memory efficiently if we assign the same variable to two different values. Object IdentityIn Python, every created object identifies uniquely in Python. Python provides the guaranteed that no two objects will have the same identifier. The built-in id() function, is used to identify the object identifier. Consider the following example.
Output: 140734982691168 140734982691168 2822056960944 We assigned the b = a, a and b both point to the same object. When we checked by the id() function it returned the same number. We reassign a to 500; then it referred to the new object identifier. Variable NamesWe have already discussed how to declare the valid variable. Variable names can be any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_). Consider the following example of valid variables names.
Output: Devansh 20 80.5 Consider the following valid variables name.
Output: A B C D E D E F G F I In the above example, we have declared a few valid variable names such as name, _name_ , etc. But it is not recommended because when we try to read code, it may create confusion. The variable name should be descriptive to make code more readable. The multi-word keywords can be created by the following method.
Multiple AssignmentPython allows us to assign a value to multiple variables in a single statement, which is also known as multiple assignments. We can apply multiple assignments in two ways, either by assigning a single value to multiple variables or assigning multiple values to multiple variables. Consider the following example. 1. Assigning single value to multiple variables Eg:
Output: 50 50 50 2. Assigning multiple values to multiple variables: Eg:
Output: 5 10 15 The values will be assigned in the order in which variables appear. Python Variable TypesThere are two types of variables in Python - Local variable and Global variable. Let's understand the following variables. Local VariableLocal variables are the variables that declared inside the function and have scope within the function. Let's understand the following example. Example -
Output: The sum is: 50 Explanation: In the above code, we declared a function named add() and assigned a few variables within the function. These variables will be referred to as the local variables which have scope only inside the function. If we try to use them outside the function, we get a following error.
Output: The sum is: 50 print(a) NameError: name 'a' is not defined We tried to use local variable outside their scope; it threw the NameError. Global VariablesGlobal variables can be used throughout the program, and its scope is in the entire program. We can use global variables inside or outside the function. A variable declared outside the function is the global variable by default. Python provides the global keyword to use global variable inside the function. If we don't use the global keyword, the function treats it as a local variable. Let's understand the following example. Example -
Output: 101 Welcome To Javatpoint Welcome To Javatpoint Explanation: In the above code, we declare a global variable x and assign a value to it. Next, we defined a function and accessed the declared variable using the global keyword inside the function. Now we can modify its value. Then, we assigned a new string value to the variable x. Now, we called the function and proceeded to print x. It printed the as newly assigned value of x. Delete a variableWe can delete the variable using the del keyword. The syntax is given below. Syntax -
In the following example, we create a variable x and assign value to it. We deleted variable x, and print it, we get the error "variable x is not defined". The variable x will no longer use in future. Example -
Output: 6 Traceback (most recent call last): File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/multiprocessing.py", line 389, in Maximum Possible Value of an Integer in PythonUnlike the other programming languages, Python doesn't have long int or float data types. It treats all integer values as an int data type. Here, the question arises. What is the maximum possible value can hold by the variable in Python? Consider the following example. Example -
Output: <class 'int'> 10000000000000000000000000000000000000000001 As we can see in the above example, we assigned a large integer value to variable x and checked its type. It printed class <int> not long int. Hence, there is no limitation number by bits and we can expand to the limit of our memory. Python doesn't have any special data type to store larger numbers. Print Single and Multiple Variables in PythonWe can print multiple variables within the single print statement. Below are the example of single and multiple printing values. Example - 1 (Printing Single Variable)
Output: 5 5 Example - 2 (Printing Multiple Variables)
Output: 5 6 1 2 3 4 5 6 7 8 Basic Fundamentals:This section contains the fundamentals of Python, such as: i)Tokens and their types. ii) Comments a)Tokens:
There are following tokens in Python:
We will discuss above the tokens in detail next tutorials.
Next TopicPython Data Types
|
Python Data TypesVariables can hold values, and every value has a data-type. Python is a dynamically typed language; hence we do not need to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.
The variable a holds integer value five and we did not define its type. Python interpreter will automatically interpret variables a as an integer type. Python enables us to check the type of the variable used in the program. Python provides us the type() function, which returns the type of the variable passed. Consider the following example to define the values of different data types and checking its type. 10 Sec Features of Java - Javatpoint
Output: <type 'int'> <type 'str'> <type 'float'> Standard data typesA variable can hold different types of values. For example, a person's name must be stored as a string whereas its id must be stored as an integer. Python provides various standard data types that define the storage method on each of them. The data types defined in Python are given below.
In this section of the tutorial, we will give a brief introduction of the above data-types. We will discuss each one of them in detail later in this tutorial. NumbersNumber stores numeric values. The integer, float, and complex values belong to a Python Numbers data-type. Python provides the type() function to know the data-type of the variable. Similarly, the isinstance() function is used to check an object belongs to a particular class. Python creates Number objects when a number is assigned to a variable. For example;
Output: The type of a <class 'int'> The type of b <class 'float'> The type of c <class 'complex'> c is complex number: True Python supports three types of numeric data.
Sequence TypeStringThe string can be defined as the sequence of characters represented in the quotation marks. In Python, we can use single, double, or triple quotes to define a string. String handling in Python is a straightforward task since Python provides built-in functions and operators to perform operations in the string. In the case of string handling, the operator + is used to concatenate two strings as the operation "hello"+" python" returns "hello python". The operator * is known as a repetition operator as the operation "Python" *2 returns 'Python Python'. The following example illustrates the string in Python. Example - 1
Output: string using double quotes A multiline string Consider the following example of string handling. Example - 2
Output: he o hello javatpointhello javatpoint hello javatpoint how are you ListPython Lists are similar to arrays in C. However, the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets []. We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings. Consider the following example.
Output: [1, 'hi', 'Python', 2] [2] [1, 'hi'] [1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2] [1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2] TupleA tuple is similar to the list in many ways. Like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses (). A tuple is a read-only data structure as we can't modify the size and value of the items of a tuple. Let's see a simple example of the tuple.
Output: <class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)
Traceback (most recent call last):
File "main.py", line 14, in <module>
t[2] = "hi";
TypeError: 'tuple' object does not support item assignment
DictionaryDictionary is an unordered set of a key-value pair of items. It is like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type, whereas value is an arbitrary Python object. The items in the dictionary are separated with the comma (,) and enclosed in the curly braces {}. Consider the following example.
Output: 1st name is Jimmy
2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
dict_keys([1, 2, 3, 4])
dict_values(['Jimmy', 'Alex', 'john', 'mike'])
BooleanBoolean type provides two built-in values, True and False. These values are used to determine the given statement true or false. It denotes by the class bool. True can be represented by any non-zero value or 'T' whereas false can be represented by the 0 or 'F'. Consider the following example.
Output: <class 'bool'> <class 'bool'> NameError: name 'false' is not defined SetPython Set is the unordered collection of the data type. It is iterable, mutable(can modify after creation), and has unique elements. In set, the order of the elements is undefined; it may return the changed sequence of the element. The set is created by using a built-in function set(), or a sequence of elements is passed in the curly braces and separated by the comma. It can contain various types of values. Consider the following example.
Output: {3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}
Next TopicPython Keywords
|
For Videos Join Our Youtube Channel: Join Now
Python KeywordsPython Keywords are special reserved words that convey a special meaning to the compiler/interpreter. Each keyword has a special meaning and a specific operation. These keywords can't be used as a variable. Following is the List of Python Keywords.
Consider the following explanation of keywords.
5. or - It is a logical operator in Python. It returns true if one of the conditions is true. Consider the following truth table.
6. not - It is a logical operator and inverts the truth value. Consider the following truth table.
7. assert - This keyword is used as the debugging tool in Python. It checks the correctness of the code. It raises an AssertionError if found any error in the code and also prints the message with an error. Example:
Output: a is dividing by Zero Runtime Exception: Traceback (most recent call last): File "/home/40545678b342ce3b70beb1224bed345f.py", line 4, in assert b != 0, "Divide by 0 error" AssertionError: Divide by 0 error 8. def - This keyword is used to declare the function in Python. If followed by the function name.
Output: 30 9. class - It is used to represents the class in Python. The class is the blueprint of the objects. It is the collection of the variable and methods. Consider the following class.
10. continue - It is used to stop the execution of the current iteration. Consider the following example.
Output: 1 3 4 11. break - It is used to terminate the loop execution and control transfer to the end of the loop. Consider the following example. Example
Output: 0 1 2 End of execution 12. If - It is used to represent the conditional statement. The execution of a particular block is decided by if statement. Consider the following example. Example
Output: I am less than 18 13. else - The else statement is used with the if statement. When if statement returns false, then else block is executed. Consider the following example. Example:
Output: Odd 14. elif - This Keyword is used to check the multiple conditions. It is short for else-if. If the previous condition is false, then check until the true condition is found. Condition the following example. Example:
Output: Enter the marks:85 Very Good 15. del - It is used to delete the reference of the object. Consider the following example. Example:
Output: 12 NameError: name 'a' is not defined 16. try, except - The try-except is used to handle the exceptions. The exceptions are run-time errors. Consider the following example. Example:
Output: division by zero 17. raise - The raise keyword is used to through the exception forcefully. Consider the following example. Example
Output: Exception: a should not exceed 2 18. finally - The finally keyword is used to create a block of code that will always be executed no matter the else block raises an error or not. Consider the following example. Example:
Output: division by zero Finally always executed 19. for, while - Both keywords are used for iteration. The for keyword is used to iterate over the sequences (list, tuple, dictionary, string). A while loop is executed until the condition returns false. Consider the following example. Example: For loop
Output: 1 2 3 4 5 Example: While loop
Output: 0 1 2 3 4 20. import - The import keyword is used to import modules in the current Python script. The module contains a runnable Python code. Example:
Output: 5 21. from - This keyword is used to import the specific function or attributes in the current Python script. Example:
Output: 5 22. as - It is used to create a name alias. It provides the user-define name while importing a module. Example:
Output: May 23. pass - The pass keyword is used to execute nothing or create a placeholder for future code. If we declare an empty class or function, it will through an error, so we use the pass keyword to declare an empty class or function. Example:
24. return - The return keyword is used to return the result value or none to called function. Example:
Output: The sum is: 40 25. is - This keyword is used to check if the two-variable refers to the same object. It returns the true if they refer to the same object otherwise false. Consider the following example. Example
Output: True False Note: A mutable data-types do not refer to the same object.26. global - The global keyword is used to create a global variable inside the function. Any function can access the global. Consider the following example. Example
Output: 30 10 27. nonlocal - The nonlocal is similar to the global and used to work with a variable inside the nested function(function inside a function). Consider the following example. Example
Output: Inner function: 30 Outer function: 30 28. lambda - The lambda keyword is used to create the anonymous function in Python. It is an inline function without a name. Consider the following example. Example
Output: 1 4 9 16 25 29. yield - The yield keyword is used with the Python generator. It stops the function's execution and returns value to the caller. Consider the following example. Example
Output: 1 2 3 30. with - The with keyword is used in the exception handling. It makes code cleaner and more readable. The advantage of using with, we don't need to call close(). Consider the following example. Example
31. None - The None keyword is used to define the null value. It is remembered that None does not indicate 0, false, or any empty data-types. It is an object of its data type, which is Consider the following example. Example:
Output: None We have covered all Python keywords. This is the brief introduction of Python Keywords. We will learn more in the upcoming tutorials.
Next TopicPython Literals
|
Python LiteralsPython Literals can be defined as data that is given in a variable or constant. Python supports the following literals: 1. String literals:String literals can be formed by enclosing a text in the quotes. We can use both single as well as double quotes to create a string. Example:
Types of Strings: There are two types of Strings supported in Python: a) Single-line String- Strings that are terminated within a single-line are known as Single line Strings. Example:
b) Multi-line String - A piece of text that is written in multiple lines is known as multiple lines string. There are two ways to create multiline strings: 1) Adding black slash at the end of each line. Example:
'hellouser' 2) Using triple quotation marks:- Example:
Output: welcome to SSSIT II. Numeric literals:Numeric Literals are immutable. Numeric literals can belong to following four different numerical types.
Example - Numeric Literals
Output: 20 100 141 301 100.5 150.0 (5+3.14j) 3.14 5.0 III. Boolean literals:A Boolean literal can have any of the two values: True or False. Example - Boolean Literals
Output: x is True y is False z is False a: 11 b: 10 IV. Special literals.Python contains one special literal i.e., None. None is used to specify to that field that is not created. It is also used for the end of lists in Python. Example - Special Literals
Output: 10 None V. Literal Collections.Python provides the four types of literal collection such as List literals, Tuple literals, Dict literals, and Set literals. List:
Example - List literals
Output: ['John', 678, 20.4, 'Peter'] ['John', 678, 20.4, 'Peter', 456, 'Andrew'] Dictionary:
Example
Output: {'name': 'Pater', 'Age': 18, 'Roll_nu': 101}
Tuple:
Example
Output: (10, 20, 'Dev', [2, 3, 4]) Set:
Example: - Set Literals
Output: {'guava', 'apple', 'papaya', 'grapes'}
Next TopicPython Operators
|
Python OperatorsThe operator can be defined as a symbol which is responsible for a particular operation between two operands. Operators are the pillars of a program on which the logic is built in a specific programming language. Python provides a variety of operators, which are described as follows.
Arithmetic OperatorsArithmetic operators are used to perform arithmetic operations between two operands. It includes + (addition), - (subtraction), *(multiplication), /(divide), %(reminder), //(floor division), and exponent (**) operators. Consider the following table for a detailed explanation of arithmetic operators.
Comparison operatorComparison operators are used to comparing the value of the two operands and returns Boolean true or false accordingly. The comparison operators are described in the following table.
Assignment OperatorsThe assignment operators are used to assign the value of the right expression to the left operand. The assignment operators are described in the following table.
Bitwise OperatorsThe bitwise operators perform bit by bit operation on the values of the two operands. Consider the following example. For example,
Logical OperatorsThe logical operators are used primarily in the expression evaluation to make a decision. Python supports the following logical operators.
Membership OperatorsPython membership operators are used to check the membership of value inside a Python data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false.
Identity OperatorsThe identity operators are used to decide whether an element certain class or type.
Operator PrecedenceThe precedence of the operators is essential to find out since it enables us to know which operator should be evaluated first. The precedence table of the operators in Python is given below.
Next TopicPython Comments
|
Python CommentsPython Comment is an essential tool for the programmers. Comments are generally used to explain the code. We can easily understand the code if it has a proper explanation. A good programmer must use the comments because in the future anyone wants to modify the code as well as implement the new module; then, it can be done easily. In the other programming language such as C++, It provides the // for single-lined comment and /*.... */ for multiple-lined comment, but Python provides the single-lined Python comment. To apply the comment in the code we use the hash(#) at the beginning of the statement or code. Let's understand the following example.
Here we have written comment over the print statement using the hash(#). It will not affect our print statement. Multiline Python CommentWe must use the hash(#) at the beginning of every line of code to apply the multiline Python comment. Consider the following example.
Example:
Output: The sum is: 15 The above code is very readable even the absolute beginners can under that what is happening in each line of the code. This is the advantage of using comments in code. We can also use the triple quotes ('''''') for multiline comment. The triple quotes are also used to string formatting. Consider the following example. Docstrings Python CommentThe docstring comment is mostly used in the module, function, class or method. It is a documentation Python string. We will explain the class/method in further tutorials. Example:
Output: Hello Joseph We can check a function's docstring by using the __doc__ attribute. Generally, four whitespaces are used as the indentation. The amount of indentation depends on user, but it must be consistent throughout that block.
Output: Output: '\n This function prints Hello Joseph\n ' Note: The docstring must be the first thing in the function; otherwise, Python interpreter cannot get the docstring.Python indentationPython indentation uses to define the block of the code. The other programming languages such as C, C++, and Java use curly braces {}, whereas Python uses an indentation. Whitespaces are used as indentation in Python. Indentation uses at the beginning of the code and ends with the unintended line. That same line indentation defines the block of the code (body of a function, loop, etc.) Generally, four whitespaces are used as the indentation. The amount of indentation depends on user, but it must be consistent throughout that block.
To indicate a block of code we indented each line of the block by the same whitespaces. Consider the following example.
Output: Enter the number: 10 Even Number Task Complete The above code, if and else are two separate code blocks. Both code blocks are indented four spaces. The print("Task Complete") statement is not indented four whitespaces and it is out of the if-else block. If the indentation is not used properly, then that will result in IndentationError.
Next TopicPython If Else
|
Python If-else statementsDecision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making. In python, decision making is performed by the following statements.
Indentation in PythonFor the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block. Generally, four spaces are given to indent the statements which are a typical amount of indentation in python. Indentation is the most used part of the python language since it declares the block of code. All the statements of one block are intended at the same level indentation. We will see how the actual indentation takes place in decision making and other stuff in python. The if statementThe if statement is used to test a particular condition and if the condition is true, it executes a block of code known as if-block. The condition of if statement can be any valid logical expression which can be either evaluated to true or false.
The syntax of the if-statement is given below.
Example 1
Output: enter the number?10 Number is even Example 2 : Program to print the largest of the three numbers.
Output: Enter a? 100 Enter b? 120 Enter c? 130 c is largest The if-else statementThe if-else statement provides an else block combined with the if statement which is executed in the false case of the condition. If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.
The syntax of the if-else statement is given below.
Example 1 : Program to check whether a person is eligible to vote or not.
Output: Enter your age? 90 You are eligible to vote !! Example 2: Program to check whether a number is even or not.
Output: enter the number?10 Number is even The elif statementThe elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in our program depending upon our need. However, using elif is optional. The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement. The syntax of the elif statement is given below.
Example 1
Output: Enter the number?15 number is not equal to 10, 50 or 100 Example 2
Next TopicPython Loops
|
Python LoopsThe flow of the programs written in any programming language is sequential by default. Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times. For this purpose, The programming languages provide various types of loops which are capable of repeating some specific code several numbers of times. Consider the following diagram to understand the working of a loop statement.
Why we use loops in python?The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code again and again, we can repeat the same code for a finite number of times. For example, if we need to print the first 10 natural numbers then, instead of using the print statement 10 times, we can print inside a loop which runs up to 10 iterations. Advantages of loopsThere are the following advantages of loops in Python.
There are the following loop statements in Python.
Next TopicPython For Loop
|
Python for loopThe for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary. The syntax of for loop in python is given below.
The for loop flowchart
For loop Using SequenceExample-1: Iterating string using for loop
Output: P y t h o n Example- 2: Program to print the table of the given number .
Output: 5 10 15 20 25 30 35 40 45 50s Example-4: Program to print the sum of the given list.
Output: The sum is: 183 For loop Using range() functionThe range() function The range() function is used to generate the sequence of the numbers. If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the range() function is given below. Syntax:
Consider the following examples: Example-1: Program to print numbers in sequence.
Output: 0 1 2 3 4 5 6 7 8 9 Example - 2: Program to print table of given number.
Output: Enter the number 10 10 * 1 = 10 10 * 2 = 20 10 * 3 = 30 10 * 4 = 40 10 * 5 = 50 10 * 6 = 60 10 * 7 = 70 10 * 8 = 80 10 * 9 = 90 10 * 10 = 100 Example-3: Program to print even number using step size in range().
Output: Enter the number 20 2 4 6 8 10 12 14 16 18 We can also use the range() function with sequence of numbers. The len() function is combined with range() function which iterate through a sequence using indexing. Consider the following example.
Output: Hello Peter Hello Joseph Hello Ricky Hello Devansh Nested for loop in pythonPython allows us to nest any number of for loops inside a for loop. The inner loop is executed n number of times for every iteration of the outer loop. The syntax is given below. Syntax
Example- 1: Nested for loop
Output: Enter the rows:5 * ** *** **** ***** Example-2: Program to number pyramid.
Output: 1 22 333 4444 55555 Using else statement with for loopUnlike other languages like C, C++, or Java, Python allows us to use the else statement with the for loop which can be executed only when all the iterations are exhausted. Here, we must notice that if the loop contains any of the break statement then the else statement will not be executed. Example 1
Output: 0 1 2 3 4 for loop completely exhausted, since there is no break. The for loop completely exhausted, since there is no break. Example 2
In the above example, the loop is broken due to the break statement; therefore, the else statement will not be executed. The statement present immediate next to else block will be executed. Output: 0 The loop is broken due to the break statement...came out of the loop. We will learn more about the break statement in next tutorial.
Next TopicPython while loop
|
Python While loopThe Python while loop allows a part of the code to be executed until the given condition returns false. It is also known as a pre-tested loop. It can be viewed as a repeating if statement. When we don't know the number of iterations then the while loop is most effective to use. The syntax is given below.
Here, the statements can be a single statement or a group of statements. The expression should be any valid Python expression resulting in true or false. The true is any non-zero value and false is 0. While loop Flowchart
Loop Control StatementsWe can change the normal sequence of while loop's execution using the loop control statement. When the while loop's execution is completed, all automatic objects defined in that scope are demolished. Python offers the following control statement to use within the while loop. 1. Continue Statement - When the continue statement is encountered, the control transfer to the beginning of the loop. Let's understand the following example. Example:
Output: Current Letter : j Current Letter : v Current Letter : p Current Letter : o Current Letter : i Current Letter : n 2. Break Statement - When the break statement is encountered, it brings control out of the loop. Example:
Output: Current Letter : j Current Letter : a Current Letter : v Current Letter : a 3. Pass Statement - The pass statement is used to declare the empty loop. It is also used to define empty class, function, and control statement. Let's understand the following example. Example -
Output: Value of i : 10 Example-1: Program to print 1 to 10 using while loop
Output: 1 2 3 4 5 6 7 8 9 10 Example -2: Program to print table of given numbers.
Output: Enter the number:10 10 X 1 = 10 10 X 2 = 20 10 X 3 = 30 10 X 4 = 40 10 X 5 = 50 10 X 6 = 60 10 X 7 = 70 10 X 8 = 80 10 X 9 = 90 10 X 10 = 100 Infinite while loopIf the condition is given in the while loop never becomes false, then the while loop will never terminate, and it turns into the infinite while loop. Any non-zero value in the while loop indicates an always-true condition, whereas zero indicates the always-false condition. This type of approach is useful if we want our program to run continuously in the loop without any disturbance. Example 1
Output: Hi! we are inside the infinite while loop Hi! we are inside the infinite while loop Example 2
Output: Enter the number:10 Entered value is 10 Enter the number:10 Entered value is 10 Enter the number:10 Entered value is 10 Infinite time Using else with while loopPython allows us to use the else statement with the while loop also. The else block is executed when the condition given in the while statement becomes false. Like for loop, if the while loop is broken using break statement, then the else block will not be executed, and the statement present after else block will be executed. The else statement is optional to use with the while loop. Consider the following example. Example 1
Example 2
Output: 1 2 In the above code, when the break statement encountered, then while loop stopped its execution and skipped the else statement. Example-3 Program to print Fibonacci numbers to given limit
Output: Enter the terms 10 Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34
Next TopicPython break statement
|
Python break statementThe break is a keyword in python which is used to bring the program control out of the loop. The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. In other words, we can say that break is used to abort the current execution of the program and the control goes to the next line after the loop. The break is commonly used in the cases where we need to break the loop for a given condition. The syntax of the break is given below.
Example 1
Output: item matched found at 2 location Example 2
Output: p y t h Example 3: break statement with while loop
Output: 0 1 2 3 4 5 6 7 8 9 came out of while loop Example 3
Output: 2 X 1 = 2 2 X 2 = 4 2 X 3 = 6 2 X 4 = 8 2 X 5 = 10 2 X 6 = 12 2 X 7 = 14 2 X 8 = 16 2 X 9 = 18 2 X 10 = 20 Do you want to continue printing the table, press 0 for no?1 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 3 X 4 = 12 3 X 5 = 15 3 X 6 = 18 3 X 7 = 21 3 X 8 = 24 3 X 9 = 27 3 X 10 = 30 Do you want to continue printing the table, press 0 for no?0
Next TopicPython continue statement
|
Python continue StatementThe continue statement in Python is used to bring the program control to the beginning of the loop. The continue statement skips the remaining lines of code inside the loop and start with the next iteration. It is mainly used for a particular condition inside the loop so that we can skip some specific code for a particular condition.The continue statement in Python is used to bring the program control to the beginning of the loop. The continue statement skips the remaining lines of code inside the loop and start with the next iteration. It is mainly used for a particular condition inside the loop so that we can skip some specific code for a particular condition. Syntax
Flow Diagram
Consider the following examples. Example 1
Output: 1 2 3 4 6 7 8 9 10 Observe the output of above code, the value 5 is skipped because we have provided the if condition using with continue statement in while loop. When it matched with the given condition then control transferred to the beginning of the while loop and it skipped the value 5 from the code. Let's have a look at another example: Example 2
Output: J a v a p o i n t Pass StatementThe pass statement is a null operation since nothing happens when it is executed. It is used in the cases where a statement is syntactically needed but we don't want to use any executable statement at its place. For example, it can be used while overriding a parent class method in the subclass but don't want to give its specific implementation in the subclass. Pass is also used where the code will be written somewhere but not yet written in the program file. Consider the following example. Example
Output: Current element: 1 Current element: 2 Current element: 3 We are inside pass block Came out of pass Current element: 4 Current element: 5 We will learn more about the pass statement in the next tutorial.
Next TopicPython Pass
|
Python PassIn Python, the pass keyword is used to execute nothing; it means, when we don't want to execute code, the pass can be used to execute empty. It is the same as the name refers to. It just makes the control to pass by without executing any code. If we want to bypass any code pass statement can be used. It is beneficial when a statement is required syntactically, but we want we don't want to execute or execute it later. The difference between the comments and pass is that, comments are entirely ignored by the Python interpreter, where the pass statement is not ignored. Suppose we have a loop, and we do not want to execute right this moment, but we will execute in the future. Here we can use the pass. Consider the following example. Example - Pass statement
Example - 2:
Output:
We can create empty class or function using the pass statement.
Next TopicPython Strings
|
Python StringTill now, we have discussed numbers as the standard data-types in Python. In this section of the tutorial, we will discuss the most popular data type in Python, i.e., string. Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes. The computer does not understand the characters; internally, it stores manipulated character as the combination of the 0's and 1's. Each character is encoded in the ASCII or Unicode character. So we can say that Python strings are also called the collection of Unicode characters. In Python, strings can be created by enclosing the character or the sequence of characters in the quotes. Python allows us to use single quotes, double quotes, or triple quotes to create the string. Consider the following example in Python to create a string. Syntax:
Here, if we check the type of the variable str using a Python script
In Python, strings are treated as the sequence of characters, which means that Python doesn't support the character data-type; instead, a single character written as 'p' is treated as the string of length 1. Creating String in PythonWe can create a string by enclosing the characters in single-quotes or double- quotes. Python also provides triple-quotes to represent the string, but it is generally used for multiline string or docstrings.
Output: Hello Python Hello Python Triple quotes are generally used for represent the multiline or docstring Strings indexing and splittingLike other languages, the indexing of the Python strings starts from 0. For example, The string "HELLO" is indexed as given in the below figure.
Consider the following example:
Output: H E L L O IndexError: string index out of range As shown in Python, the slice operator [] is used to access the individual characters of the string. However, we can use the : (colon) operator in Python to access the substring from the given string. Consider the following example.
Here, we must notice that the upper range given in the slice operator is always exclusive i.e., if str = 'HELLO' is given, then str[1:3] will always include str[1] = 'E', str[2] = 'L' and nothing else. Consider the following example:
Output: JAVATPOINT AVAT VA JAV TPO We can do the negative slicing in the string; it starts from the rightmost character, which is indicated as -1. The second rightmost index indicates -2, and so on. Consider the following image.
Consider the following example
Output: T I NT OIN ATPOI TNIOPTAVAJ IndexError: string index out of range Reassigning StringsUpdating the content of the strings is as easy as assigning it to a new string. The string object doesn't support item assignment i.e., A string can only be replaced with new string since its content cannot be partially replaced. Strings are immutable in Python. Consider the following example. Example 1
Output: Traceback (most recent call last): File "12.py", line 2, in <module> str[0] = "h"; TypeError: 'str' object does not support item assignment However, in example 1, the string str can be assigned completely to a new content as specified in the following example. Example 2
Output: HELLO hello Deleting the StringAs we know that strings are immutable. We cannot delete or remove the characters from the string. But we can delete the entire string using the del keyword.
Output: TypeError: 'str' object doesn't support item deletion Now we are deleting entire string.
Output: NameError: name 'str1' is not defined String Operators
ExampleConsider the following example to understand the real use of Python operators.
Output: HelloHelloHello Hello world o ll False False C://python37 The string str : Hello Python String FormattingEscape SequenceLet's suppose we need to write the text as - They said, "Hello what's going on?"- the given statement can be written in single quotes or double quotes but it will raise the SyntaxError as it contains both single and double-quotes. ExampleConsider the following example to understand the real use of Python operators.
Output: SyntaxError: invalid syntax We can use the triple quotes to accomplish this problem but Python provides the escape sequence. The backslash(/) symbol denotes the escape sequence. The backslash can be followed by a special character and it interpreted differently. The single quotes inside the string must be escaped. We can apply the same as in the double quotes. Example -
Output: They said, "What's there?" They said, "What's going on?" They said, "What's going on?" The list of an escape sequence is given below:
Here is the simple example of escape sequence.
Output: C:\Users\DEVANSH SHARMA\Python32\Lib This is the multiline quotes This is HEX representation We can ignore the escape sequence from the given string by using the raw string. We can do this by writing r or R in front of the string. Consider the following example.
Output: C:\\Users\\DEVANSH SHARMA\\Python32 The format() methodThe format() method is the most flexible and useful method in formatting strings. The curly braces {} are used as the placeholder in the string and replaced by the format() method argument. Let's have a look at the given an example:
Output: Devansh and Abhishek both are the best friend Rohit and Virat best players James,Peter,Ricky Python String Formatting Using % OperatorPython allows us to use the format specifiers used in C's printf statement. The format specifiers in Python are treated in the same way as they are treated in C. However, Python provides an additional operator %, which is used as an interface between the format specifiers and their values. In other words, we can say that it binds the format specifiers to the values. Consider the following example.
Output: Hi I am Integer ... My value is 10 Hi I am float ... My value is 1.290000 Hi I am string ... My value is Devansh Python String functionsPython provides various in-built functions that are used for string handling. Many String fun
Next TopicPython Lists
|
Python ListA list in Python is used to store the sequence of various types of data. Python lists are mutable type its mean we can modify its element after it created. However, Python consists of six data-types that are capable to store the sequences, but the most common and reliable type is the list. A list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets []. A list can be define as below
IIf we try to print the type of L1, L2, and L3 using type() function then it will come out to be a list.
Output: <class 'list'> <class 'list'> Characteristics of ListsThe list has the following characteristics:
Let's check the first statement that lists are the ordered.
Output: False Both lists have consisted of the same elements, but the second list changed the index position of the 5th element that violates the order of lists. When compare both lists it returns the false. Lists maintain the order of the element for the lifetime. That's why it is the ordered collection of objects.
Output: True Let's have a look at the list example in detail.
Output: printing employee data... Name : John, ID: 102, Country: USA printing departments... Department 1: Name: CS, ID: 11 Department 2: Name: IT, ID: 11 HOD Details .... CS HOD Name: Mr. Holding, Id: 10 IT HOD Name: Mr. Bewon, Id: 11 <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> In the above example, we have created the lists which consist of the employee and department details and printed the corresponding details. Observe the above code to understand the concept of the list better. List indexing and splittingThe indexing is processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator []. The index starts from 0 and goes to length - 1. The first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on.
We can get the sub-list of the list using the following syntax.
Consider the following example:
Output: 1 2 3 4 [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 7] [3, 4, 5] [2, 4, 6] Unlike other languages, Python provides the flexibility to use the negative indexing also. The negative indices are counted from the right. The last element (rightmost) of the list has the index -1; its adjacent left element is present at the index -2 and so on until the left-most elements are encountered.
Let's have a look at the following example where we will use negative indexing to access the elements of the list.
Output: 5 [3, 4, 5] [1, 2, 3, 4] [3, 4] As we discussed above, we can get an element by using negative indexing. In the above code, the first print statement returned the rightmost element of the list. The second print statement returned the sub-list, and so on. Updating List valuesLists are the most versatile data structures in Python since they are mutable, and their values can be updated by using the slice and assignment operator. Python also provides append() and insert() methods, which can be used to add values to the list. Consider the following example to update the values inside the list.
Output: [1, 2, 3, 4, 5, 6] [1, 2, 10, 4, 5, 6] [1, 89, 78, 4, 5, 6] [1, 89, 78, 4, 5, 25] The list elements can also be deleted by using the del keyword. Python also provides us the remove() method if we do not know which element is to be deleted from the list. Consider the following example to delete the list elements.
Output: [1, 2, 3, 4, 5, 6] [1, 2, 10, 4, 5, 6] [1, 89, 78, 4, 5, 6] [1, 89, 78, 4, 5, 25] Python List OperationsThe concatenation (+) and repetition (*) operators work in the same way as they were working with the strings. Let's see how the list responds to various operators.
Iterating a ListA list can be iterated by using a for - in loop. A simple list containing four strings, which can be iterated as follows.
Output: John David James Jonathan Adding elements to the listPython provides append() function which is used to add an element to the list. However, the append() function can only add value to the end of the list. Consider the following example in which, we are taking the elements of the list from the user and printing the list on the console.
Output: Enter the number of elements in the list:5 Enter the item:25 Enter the item:46 Enter the item:12 Enter the item:75 Enter the item:42 printing the list items 25 46 12 75 42 Removing elements from the listPython provides the remove() function which is used to remove the element from the list. Consider the following example to understand this concept. Example -
Output: printing original list: 0 1 2 3 4 printing the list after the removal of first element... 0 1 3 4 Python List Built-in functionsPython provides the following built-in functions, which can be used with the lists.
Let's have a look at the few list examples. Example: 1- Write the program to remove the duplicate element of the list.
Output: [1, 2, 3, 55, 98, 65, 13, 29] Example:2- Write a program to find the sum of the element in the list.
Output: The sum is: 67 Example: 3- Write the program to find the lists consist of at least one common element.
Output: The common element is: 2
Next TopicPython Tuples
|
Python TuplePython Tuple is used to store the sequence of immutable Python objects. The tuple is similar to lists since the value of the items stored in the list can be changed, whereas the tuple is immutable, and the value of the items stored in the tuple cannot be changed. Creating a tupleA tuple can be written as the collection of comma-separated (,) values enclosed with the small () brackets. The parentheses are optional but it is good practice to use. A tuple can be defined as follows.
Output: <class 'tuple'> <class 'tuple'> <class 'tuple'> Note: The tuple which is created without using parentheses is also known as tuple packing.An empty tuple can be created as follows.
T4 = ()
Creating a tuple with single element is slightly different. We will need to put comma after the element to declare the tuple.
Output: <class 'str'> <class 'tuple'> A tuple is indexed in the same way as the lists. The items in the tuple can be accessed by using their specific index value. Consider the following example of tuple: Example - 1
Output: (10, 20, 30, 40, 50, 60) tuple1[0] = 10 tuple1[1] = 20 tuple1[2] = 30 tuple1[3] = 40 tuple1[4] = 50 tuple1[5] = 60 Example - 2
Output: Enter the tuple elements ...123456
('1', '2', '3', '4', '5', '6')
tuple1[0] = 1
tuple1[1] = 2
tuple1[2] = 3
tuple1[3] = 4
tuple1[4] = 5
tuple1[5] = 6
A tuple is indexed in the same way as the lists. The items in the tuple can be accessed by using their specific index value. We will see all these aspects of tuple in this section of the tutorial. Tuple indexing and slicingThe indexing and slicing in the tuple are similar to lists. The indexing in the tuple starts from 0 and goes to length(tuple) - 1. The items in the tuple can be accessed by using the index [] operator. Python also allows us to use the colon operator to access multiple items in the tuple. Consider the following image to understand the indexing and slicing in detail.
Consider the following example:
Output: 1 2 3 tuple index out of range In the above code, the tuple has 7 elements which denote 0 to 6. We tried to access an element outside of tuple that raised an IndexError.
Output: (2, 3, 4, 5, 6, 7) (1, 2, 3, 4) (1, 2, 3, 4) (1, 3, 5) Negative IndexingThe tuple element can also access by using negative indexing. The index of -1 denotes the rightmost element and -2 to the second last item and so on. The elements from left to right are traversed using the negative indexing. Consider the following example:
Output: 5 2 (3, 4) (1, 2, 3, 4) (4, 5) Deleting TupleUnlike lists, the tuple items cannot be deleted by using the del keyword as tuples are immutable. To delete an entire tuple, we can use the del keyword with the tuple name. Consider the following example.
Output: (1, 2, 3, 4, 5, 6) Traceback (most recent call last): File "tuple.py", line 4, in <module> print(tuple1) NameError: name 'tuple1' is not defined Basic Tuple operationsThe operators like concatenation (+), repetition (*), Membership (in) works in the same way as they work with the list. Consider the following table for more detail. Let's say Tuple t = (1, 2, 3, 4, 5) and Tuple t1 = (6, 7, 8, 9) are declared.
Python Tuple inbuilt functions
Where use tuple?Using tuple instead of list is used in the following scenario. 1. Using tuple instead of list gives us a clear idea that tuple data is constant and must not be changed. 2. Tuple can simulate a dictionary without keys. Consider the following nested structure, which can be used as a dictionary.
List vs. Tuple
Next TopicPython Set
|
Python List Vs TupleIn this tutorial, we will learn the important difference between the list and tuples and how both are playing a significant role in Python. Lists and Tuples are used to store one or more Python objects or data-types sequentially. Both can store any data such as integer, float, string, and dictionary. Lists and Tuples are similar in most factors but here we will describe the main difference between them. Let's discuss the main differences in the following points. Representation DifferencesThe representation of the Lists and tuple is marginally different. List are commonly enclosed with the square bracket [], and elements are comma-separated element. Tuples are enclosed with parenthesis (), and elements are separated by the comma. The parenthesis is optional to use, and these types of tuples are called tuple packing. Consider the following example.
Output: <class 'list'> <class 'tuple'> In the above program, we defined a list1 variable which holds a list of different data type from index 0 to 4. We defined another variable tuple1, which holds a tuple of different data types. It is enclosed by the (). Mutable Lists and Immutable TuplesIt is the most important difference between list and tuple whereas lists are mutable, and tuples are immutable. The lists are mutable which means the Python object can be modified after creation, whereas tuples can't be modified after creation. Consider the given an example.
Output: ['Peter', 'Joseph', 'Mathew', 'Ricky'] Now we are changing 0th index element "Peter" to "Samson".
Output: ['Samson', 'Joseph', 'Mathew', 'Ricky'] Now we create a tuple and do the same thing.
Output: (10, 20, 'JavaTpoint', 30, 40)
Output: TypeError Traceback (most recent call last) <ipython-input-5-52b2981fae12> in <module> ----> 1 a[0] = 50 TypeError: 'tuple' object does not support item assignment We get an error while changing the 1st element of the tuple because of immutability. It does not support item assignment. DebuggingThe tuples are easy to debug in a big project because of its immutability. If we have a small project or less number of data, then lists play an effective role. Let's consider the following example:
Output: [6, 9, 4, 'JavaToint', 7, 0, 1] In the above code, we did b = a; here we are not copying the list object from b to a. The b referred to the address of the list a. It means if we make the change in the b then that will reflect the same as in list a, and it makes debugging easy. But it is hard for the significant project where Python objects may have multiple references. It will be very complicated to track those changes in lists but immutable object tuple can't change after created. So tuples are easy to debug. Functions SupportThe tuples support less operation than the list. The inbuilt dir(object) is used to get all the supported functions for the list and tuple.
Output: ['__add__','__class__','__contains__','__delattr__','__delitem__','__dir_, '__doc__','__eq__','__format__', '__get__','__getattribute__','__getitem_' '__gt__','__hash__','__iadd__','__imul__','__init__','__init_subclass__''__iter__','__le__','__len__','__lt__','__mul__', '__ne__','__new__', '__reduce__', '__reduce_ex__','__repr__','__reversed__','__rmul__','__setattr__','__setitem__','__sizeof__','__str__','__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Output: ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index'] Memory EfficientThe tuples are more memory efficient than the list because tuple has less built-in operations. Lists are suitable for the fewer elements whereas tuples are a bit faster than the list for the huge amount of data.
Output: Tuple size = 168 List size = 216 Conclusion
Next TopicPython Set
|
Python SetA Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation. Unlike other collections in Python, there is no index attached to the elements of the set, i.e., we cannot directly access any element of the set by the index. However, we can print them all together, or we can get the list of elements by looping through the set. Creating a setThe set can be created by enclosing the comma-separated immutable items with the curly braces {}. Python also provides the set() method, which can be used to create the set by the passed sequence. Example 1: Using curly braces
Output: {'Friday', 'Tuesday', 'Monday', 'Saturday', 'Thursday', 'Sunday', 'Wednesday'}
<class 'set'>
looping through the set elements ...
Friday
Tuesday
Monday
Saturday
Thursday
Sunday
Wednesday
Example 2: Using set() method
Output: {'Friday', 'Wednesday', 'Thursday', 'Saturday', 'Monday', 'Tuesday', 'Sunday'}
<class 'set'>
looping through the set elements ...
Friday
Wednesday
Thursday
Saturday
Monday
Tuesday
Sunday
It can contain any type of element such as integer, float, tuple etc. But mutable elements (list, dictionary, set) can't be a member of set. Consider the following example.
Output: <class 'set'>
Traceback (most recent call last)
<ipython-input-5-9605bb6fbc68> in <module>
4
5 #Creating a set which holds mutable elements
----> 6 set2 = {1,2,3,["Javatpoint",4]}
7 print(type(set2))
TypeError: unhashable type: 'list'
In the above code, we have created two sets, the set set1 have immutable elements and set2 have one mutable element as a list. While checking the type of set2, it raised an error, which means set can contain only immutable elements. Creating an empty set is a bit different because empty curly {} braces are also used to create a dictionary as well. So Python provides the set() method used without an argument to create an empty set.
Output: <class 'dict'> <class 'set'> Let's see what happened if we provide the duplicate element to the set.
Output: Return set with unique elements: {1, 2, 4, 5, 8, 9, 10}
In the above code, we can see that set5 consisted of multiple duplicate elements when we printed it remove the duplicity from the set. Adding items to the setPython provides the add() method and update() method which can be used to add some particular item to the set. The add() method is used to add a single element whereas the update() method is used to add multiple elements to the set. Consider the following example. Example: 1 - Using add() method
Output: printing the original set ...
{'February', 'May', 'April', 'March', 'June', 'January'}
Adding other months to the set...
Printing the modified set...
{'February', 'July', 'May', 'April', 'March', 'August', 'June', 'January'}
looping through the set elements ...
February
July
May
April
March
August
June
January
To add more than one item in the set, Python provides the update() method. It accepts iterable as an argument. Consider the following example. Example - 2 Using update() function
Output: printing the original set ...
{'January', 'February', 'April', 'May', 'June', 'March'}
updating the original set ...
printing the modified set ...
{'January', 'February', 'April', 'August', 'October', 'May', 'June', 'July', 'September', 'March'}
Removing items from the setPython provides the discard() method and remove() method which can be used to remove the items from the set. The difference between these function, using discard() function if the item does not exist in the set then the set remain unchanged whereas remove() method will through an error. Consider the following example. Example-1 Using discard() method
Output: printing the original set ...
{'February', 'January', 'March', 'April', 'June', 'May'}
Removing some months from the set...
Printing the modified set...
{'February', 'March', 'April', 'June'}
looping through the set elements ...
February
March
April
June
Python provides also the remove() method to remove the item from the set. Consider the following example to remove the items using remove() method. Example-2 Using remove() function
Output: printing the original set ...
{'February', 'June', 'April', 'May', 'January', 'March'}
Removing some months from the set...
Printing the modified set...
{'February', 'June', 'April', 'March'}
We can also use the pop() method to remove the item. Generally, the pop() method will always remove the last item but the set is unordered, we can't determine which element will be popped from set. Consider the following example to remove the item from the set using pop() method.
Output: printing the original set ...
{'June', 'January', 'May', 'April', 'February', 'March'}
Removing some months from the set...
Printing the modified set...
{'May', 'April', 'February', 'March'}
In the above code, the last element of the Month set is March but the pop() method removed the June and January because the set is unordered and the pop() method could not determine the last element of the set. Python provides the clear() method to remove all the items from the set. Consider the following example.
Output: printing the original set ...
{'January', 'May', 'June', 'April', 'March', 'February'}
Removing all the items from the set...
Printing the modified set...
set()
Difference between discard() and remove()Despite the fact that discard() and remove() method both perform the same task, There is one main difference between discard() and remove(). If the key to be deleted from the set using discard() doesn't exist in the set, the Python will not give the error. The program maintains its control flow. On the other hand, if the item to be deleted from the set using remove() doesn't exist in the set, the Python will raise an error. Consider the following example. Example-
Output: printing the original set ...
{'March', 'January', 'April', 'June', 'February', 'May'}
Removing items through discard() method...
printing the modified set...
{'March', 'January', 'April', 'June', 'February', 'May'}
Removing items through remove() method...
Traceback (most recent call last):
File "set.py", line 9, in
Months.remove("Jan")
KeyError: 'Jan'
Python Set OperationsSet can be performed mathematical operation such as union, intersection, difference, and symmetric difference. Python provides the facility to carry out these operations with operators or methods. We describe these operations as follows. Union of two SetsThe union of two sets is calculated by using the pipe (|) operator. The union of the two sets contains all the items that are present in both the sets.
Consider the following example to calculate the union of two sets. Example 1: using union | operator
Output: {'Friday', 'Sunday', 'Saturday', 'Tuesday', 'Wednesday', 'Monday', 'Thursday'}
Python also provides the union() method which can also be used to calculate the union of two sets. Consider the following example. Example 2: using union() method
Output: {'Friday', 'Monday', 'Tuesday', 'Thursday', 'Wednesday', 'Sunday', 'Saturday'}
Intersection of two setsThe intersection of two sets can be performed by the and & operator or the intersection() function. The intersection of the two sets is given as the set of the elements that common in both sets.
Consider the following example. Example 1: Using & operator
Output: {'Monday', 'Tuesday'}
Example 2: Using intersection() method
Output: {'Martin', 'David'}
Example 3:
Output: {1,2,5}
The intersection_update() methodThe intersection_update() method removes the items from the original set that are not present in both the sets (all the sets if more than one are specified). The intersection_update() method is different from the intersection() method since it modifies the original set by removing the unwanted items, on the other hand, the intersection() method returns a new set. Consider the following example.
Output: {'castle'}
Difference between the two setsThe difference of two sets can be calculated by using the subtraction (-) operator or intersection() method. Suppose there are two sets A and B, and the difference is A-B that denotes the resulting set will be obtained that element of A, which is not present in the set B.
Consider the following example. Example 1 : Using subtraction ( - ) operator
Output: {'Thursday', 'Wednesday'}
Example 2 : Using difference() method
Output: {'Thursday', 'Wednesday'}
Symmetric Difference of two setsThe symmetric difference of two sets is calculated by ^ operator or symmetric_difference() method. Symmetric difference of sets, it removes that element which is present in both sets. Consider the following example:
Example - 1: Using ^ operator
Output: {3, 4, 5, 6, 8, 9, 10}
Example - 2: Using symmetric_difference() method
Output: {3, 4, 5, 6, 8, 9, 10}
Set comparisonsPython allows us to use the comparison operators i.e., <, >, <=, >= , == with the sets by using which we can check whether a set is a subset, superset, or equivalent to other set. The boolean true or false is returned depending upon the items present inside the sets. Consider the following example.
Output: True False False FrozenSetsThe frozen sets are the immutable form of the normal sets, i.e., the items of the frozen set cannot be changed and therefore it can be used as a key in the dictionary. The elements of the frozen set cannot be changed after the creation. We cannot change or append the content of the frozen sets by using the methods like add() or remove(). The frozenset() method is used to create the frozenset object. The iterable sequence is passed into this method which is converted into the frozen set as a return type of the method. Consider the following example to create the frozen set.
Output: <class 'frozenset'> printing the content of frozen set... 1 2 3 4 5 Traceback (most recent call last): File "set.py", line 6, in <module> Frozenset.add(6) #gives an error since we can change the content of Frozenset after creation AttributeError: 'frozenset' object has no attribute 'add' Frozenset for the dictionaryIf we pass the dictionary as the sequence inside the frozenset() method, it will take only the keys from the dictionary and returns a frozenset that contains the key of the dictionary as its elements. Consider the following example.
Output: <class 'dict'> <class 'frozenset'> Name Country ID Set Programming ExampleExample - 1: Write a program to remove the given number from the set.
Output: Enter the number you want to remove:12
After Removing: {1, 2, 3, 4, 5, 6, 24}
Example - 2: Write a program to add multiple elements to the set.
Output: {1, 2, 4, 'Apple', 'John', 'CS', 'Mango', 'Grapes'}
Example - 3: Write a program to find the union between two set.
Output: {96, 65, 2, 'Joseph', 1, 'Peter', 59}
Example- 4: Write a program to find the intersection between two sets.
Output: {56, 23}
Example - 5: Write the program to add element to the frozenset.
Output: TypeError: 'frozenset' object does not support item assignment Above code raised an error because frozensets are immutable and can't be changed after creation. Example - 6: Write the program to find the issuperset, issubset and superset.
Output: False False True True Python Built-in set methodsPython contains the following methods to be used with the sets.
Next TopicPython Dictionary
|
Python DictionaryPython Dictionary is used to store the data in a key-value pair format. The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key. It is the mutable data-structure. The dictionary is defined into element Keys and values.
In other words, we can say that a dictionary is the collection of key-value pairs where the value can be any Python object. In contrast, the keys are the immutable Python object, i.e., Numbers, string, or tuple. Creating the dictionaryThe dictionary can be created by using multiple key-value pairs enclosed with the curly brackets {}, and each key is separated from its value by the colon (:).The syntax to define the dictionary is given below. Syntax:
In the above dictionary Dict, The keys Name and Age are the string that is an immutable object. Let's see an example to create a dictionary and print its content.
Output <class 'dict'>
Printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 25000, 'Company': 'GOOGLE'}
Python provides the built-in function dict() method which is also used to create dictionary. The empty curly braces {} is used to create empty dictionary.
Output: Empty Dictionary:
{}
Create Dictionary by using dict():
{1: 'Java', 2: 'T', 3: 'Point'}
Dictionary with each item as a pair:
{1: 'Devansh', 2: 'Sharma'}
Accessing the dictionary valuesWe have discussed how the data can be accessed in the list and tuple by using the indexing. However, the values can be accessed in the dictionary by using the keys as keys are unique in the dictionary. The dictionary values can be accessed in the following way.
Output: <class 'dict'> printing Employee data .... Name : John Age : 29 Salary : 25000 Company : GOOGLE Python provides us with an alternative to use the get() method to access the dictionary values. It would give the same result as given by the indexing. Adding dictionary valuesThe dictionary is a mutable data type, and its values can be updated by using the specific keys. The value can be updated along with key Dict[key] = value. The update() method is also used to update an existing value. Note: If the key-value already present in the dictionary, the value gets updated. Otherwise, the new keys added in the dictionary. Let's see an example to update the dictionary values. Example - 1:
Output: Empty Dictionary:
{}
Dictionary after adding 3 elements:
{0: 'Peter', 2: 'Joseph', 3: 'Ricky'}
Dictionary after adding 3 elements:
{0: 'Peter', 2: 'Joseph', 3: 'Ricky', 'Emp_ages': (20, 33, 24)}
Updated key value:
{0: 'Peter', 2: 'Joseph', 3: 'JavaTpoint', 'Emp_ages': (20, 33, 24)}
Example - 2:
Output: Empty Dictionary:
{}
Dictionary after adding 3 elements:
{0: 'Peter', 2: 'Joseph', 3: 'Ricky'}
Dictionary after adding 3 elements:
{0: 'Peter', 2: 'Joseph', 3: 'Ricky', 'Emp_ages': (20, 33, 24)}
Updated key value:
{0: 'Peter', 2: 'Joseph', 3: 'JavaTpoint', 'Emp_ages': (20, 33, 24)}
Deleting elements using del keywordThe items of the dictionary can be deleted by using the del keyword as given below.
Output: <class 'dict'>
printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 25000, 'Company': 'GOOGLE'}
Deleting some of the employee data
printing the modified information
{'Age': 29, 'salary': 25000}
Deleting the dictionary: Employee
Lets try to print it again
NameError: name 'Employee' is not defined
The last print statement in the above code, it raised an error because we tried to print the Employee dictionary that already deleted.
The pop() method accepts the key as an argument and remove the associated value. Consider the following example.
Output: {1: 'JavaTpoint', 2: 'Peter'}
Python also provides a built-in methods popitem() and clear() method for remove elements from the dictionary. The popitem() removes the arbitrary element from a dictionary, whereas the clear() method removes all elements to the whole dictionary. Iterating DictionaryA dictionary can be iterated using for loop as given below. Example 1# for loop to print all the keys of a dictionary
Output: Name Age salary Company Example 2#for loop to print all the values of the dictionary
Output: John 29 25000 GOOGLE Example - 3#for loop to print the values of the dictionary by using values() method.
Output: John 29 25000 GOOGLE Example 4#for loop to print the items of the dictionary by using items() method.
Output: ('Name', 'John')
('Age', 29)
('salary', 25000)
('Company', 'GOOGLE')
Properties of Dictionary keys1. In the dictionary, we cannot store multiple values for the same keys. If we pass more than one value for a single key, then the value which is last assigned is considered as the value of the key. Consider the following example.
Output: Name John Age 29 Salary 25000 Company GOOGLE 2. In python, the key cannot be any mutable object. We can use numbers, strings, or tuples as the key, but we cannot use any mutable object like the list as the key in the dictionary. Consider the following example.
Output: Traceback (most recent call last):
File "dictionary.py", line 1, in
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE",[100,201,301]:"Department ID"}
TypeError: unhashable type: 'list'
Built-in Dictionary functionsThe built-in python dictionary methods along with the description are given below.
Built-in Dictionary methodsThe built-in python dictionary methods along with the description are given below.
Next TopicPython Functions
|
Python FunctionFunctions are the most important aspect of an application. A function can be defined as the organized block of reusable code, which can be called whenever required. Python allows us to divide a large program into the basic building blocks known as a function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the Python program. The Function helps to programmer to break the program into the smaller part. It organizes the code very effectively and avoids the repetition of the code. As the program grows, function makes the program more organized. Python provide us various inbuilt functions like range() or print(). Although, the user can create its functions, which can be called user-defined functions. There are mainly two types of functions.
In this tutorial, we will discuss the user define functions. Advantage of Functions in PythonThere are the following advantages of Python functions.
Creating a FunctionPython provides the def keyword to define the function. The syntax of the define function is given below. Syntax:
Let's understand the syntax of functions definition.
Function CallingIn Python, after the function is created, we can call it from another function. A function must be defined before the function call; otherwise, the Python interpreter gives an error. To call the function, use the function name followed by the parentheses. Consider the following example of a simple example that prints the message "Hello World".
Output: hello world The return statementThe return statement is used at the end of the function and returns the result of the function. It terminates the function execution and transfers the result where the function is called. The return statement cannot be used outside of the function. Syntax
It can contain the expression which gets evaluated and value is returned to the caller function. If the return statement has no expression or does not exist itself in the function then it returns the None object. Consider the following example: Example 1
Output: The sum is: 30 In the above code, we have defined the function named sum, and it has a statement c = a+b, which computes the given values, and the result is returned by the return statement to the caller function. Example 2 Creating function without return statement
Output: None In the above code, we have defined the same function without the return statement as we can see that the sum() function returned the None object to the caller function. Arguments in functionThe arguments are types of information which can be passed into the function. The arguments are specified in the parentheses. We can pass any number of arguments, but they must be separate them with a comma. Consider the following example, which contains a function that accepts a string as the argument. Example 1
Output: Hi Devansh Example 2
Output: Enter a: 10 Enter b: 20 Sum = 30 Call by reference in PythonIn Python, call by reference means passing the actual value as an argument in the function. All the functions are called by reference, i.e., all the changes made to the reference inside the function revert back to the original value referred by the reference. Example 1 Passing Immutable Object (List)
Output: list inside function = [10, 30, 40, 50, 20, 30] list outside function = [10, 30, 40, 50, 20, 30] Example 2 Passing Mutable Object (String)
Output: printing the string inside function : Hi I am there Hows you printing the string outside function : Hi I am there Types of argumentsThere may be several types of arguments which can be passed at the time of function call.
Required ArgumentsTill now, we have learned about function calling in Python. However, we can provide the arguments at the time of the function call. As far as the required arguments are concerned, these are the arguments which are required to be passed at the time of function calling with the exact match of their positions in the function call and function definition. If either of the arguments is not provided in the function call, or the position of the arguments is changed, the Python interpreter will show the error. Consider the following example. Example 1
Output: Enter the name: John Hi John Example 2
Output: Enter the principle amount: 5000 Enter the rate of interest: 5 Enter the time in years: 3 Simple Interest: 750.0 Example 3
Output: TypeError: calculate() missing 1 required positional argument: 'b' Default ArgumentsPython allows us to initialize the arguments at the function definition. If the value of any of the arguments is not provided at the time of function call, then that argument can be initialized with the value given in the definition even if the argument is not specified at the function call. Example 1
Output: My name is John and age is 22 Example 2
Output: My name is john and age is 22 My name is David and age is 10 Variable-length Arguments (*args)In large projects, sometimes we may not know the number of arguments to be passed in advance. In such cases, Python provides us the flexibility to offer the comma-separated values which are internally treated as tuples at the function call. By using the variable-length arguments, we can pass any number of arguments. However, at the function definition, we define the variable-length argument using the *args (star) as *<variable - name >. Consider the following example. Example
Output: type of passed argument is <class 'tuple'> printing the passed arguments... john David smith nick In the above code, we passed *names as variable-length argument. We called the function and passed values which are treated as tuple internally. The tuple is an iterable sequence the same as the list. To print the given values, we iterated *arg names using for loop. Keyword arguments(**kwargs)Python allows us to call the function with the keyword arguments. This kind of function call will enable us to pass the arguments in the random order. The name of the arguments is treated as the keywords and matched in the function calling and definition. If the same match is found, the values of the arguments are copied in the function definition. Consider the following example. Example 1
Output: printing the message with John and hello Example 2 providing the values in different order at the calling
Output: Simple Interest: 1900.0 If we provide the different name of arguments at the time of function call, an error will be thrown. Consider the following example. Example 3
Output: TypeError: simple_interest() got an unexpected keyword argument 'time' The Python allows us to provide the mix of the required arguments and keyword arguments at the time of function call. However, the required argument must not be given after the keyword argument, i.e., once the keyword argument is encountered in the function call, the following arguments must also be the keyword arguments. Consider the following example. Example 4
Output: printing the message with John , hello ,and David The following example will cause an error due to an in-proper mix of keyword and required arguments being passed in the function call. Example 5
Output: SyntaxError: positional argument follows keyword argument Python provides the facility to pass the multiple keyword arguments which can be represented as **kwargs. It is similar as the *args but it stores the argument in the dictionary format. This type of arguments is useful when we do not know the number of arguments in advance. Consider the following example: Example 6: Many arguments using Keyword argument
Output: {'a': 'Apple'}
{'fruits': 'Orange', 'Vagitables': 'Carrot'}
Scope of variablesThe scopes of the variables depend upon the location where the variable is being declared. The variable declared in one part of the program may not be accessible to the other parts. In python, the variables are defined with the two types of scopes.
The variable defined outside any function is known to have a global scope, whereas the variable defined inside a function is known to have a local scope. Consider the following example. Example 1 Local Variable
Output: hello !! I am going to print a message. File "/root/PycharmProjects/PythonTest/Test1.py", line 5, in print(message) NameError: name 'message' is not defined Example 2 Global Variable
Output: The sum is 60 Value of sum outside the function: 0
Next TopicPython Built-in Functions
|
Python Built-in FunctionsThe Python built-in functions are defined as the functions whose functionality is pre-defined in Python. The python interpreter has several functions that are always present for use. These functions are known as Built-in Functions. There are several built-in functions in Python which are listed below: Python abs() FunctionThe python abs() function is used to return the absolute value of a number. It takes only one argument, a number whose absolute value is to be returned. The argument can be an integer and floating-point number. If the argument is a complex number, then, abs() returns its magnitude. Python abs() Function Example
Output: Absolute value of -20 is: 20 Absolute value of -20.83 is: 20.83 Python all() FunctionThe python all() function accepts an iterable object (such as list, dictionary, etc.). It returns true if all items in passed iterable are true. Otherwise, it returns False. If the iterable object is empty, the all() function returns True. Python all() Function Example
Output: True False False False True Python bin() FunctionThe python bin() function is used to return the binary representation of a specified integer. A result always starts with the prefix 0b. Python bin() Function Example
Output: 0b1010 Python bool()The python bool() converts a value to boolean(True or False) using the standard truth testing procedure. Python bool() Example
Output: [] is False [0] is True 0.0 is False None is False True is True Easy string is True Python bytes()The python bytes() in Python is used for returning a bytes object. It is an immutable version of the bytearray() function. It can create empty bytes object of the specified size. Python bytes() Example
Output: b ' Hello World.' Python callable() FunctionA python callable() function in Python is something that can be called. This built-in function checks and returns true if the object passed appears to be callable, otherwise false. Python callable() Function Example
Output: False Python compile() FunctionThe python compile() function takes source code as input and returns a code object which can later be executed by exec() function. Python compile() Function Example
Output: <class 'code'> sum = 15 Python exec() FunctionThe python exec() function is used for the dynamic execution of Python program which can either be a string or object code and it accepts large blocks of code, unlike the eval() function which only accepts a single expression. Python exec() Function Example
Output: True 12 Python sum() FunctionAs the name says, python sum() function is used to get the sum of numbers of an iterable, i.e., list. Python sum() Function Example
Output: 7 17 Python any() FunctionThe python any() function returns true if any item in an iterable is true. Otherwise, it returns False. Python any() Function Example
Output: True False True False Python ascii() FunctionThe python ascii() function returns a string containing a printable representation of an object and escapes the non-ASCII characters in the string using \x, \u or \U escapes. Python ascii() Function Example
Output: 'Python is interesting' 'Pyth\xf6n is interesting' Pythön is interesting Python bytearray()The python bytearray() returns a bytearray object and can convert objects into bytearray objects, or create an empty bytearray object of the specified size. Python bytearray() Example
Output: bytearray(b'Python is a programming language.') Python eval() Function
The python eval() function parses the expression passed to it and runs python expression(code) within the program. Python eval() Function Example
Output: 9 Python float()The python float() function returns a floating-point number from a number or string. Python float() Example
Output: 9.0 8.19 -24.27 -17.19 ValueError: could not convert string to float: 'xyz' Python format() FunctionThe python format() function returns a formatted representation of the given value. Python format() Function Example
Output: 123 123.456790 1100 Python frozenset()The python frozenset() function returns an immutable frozenset object initialized with elements from the given iterable. Python frozenset() Example
Output: Frozen set is: frozenset({'o', 'm', 's', 'r', 't'})
Empty frozen set is: frozenset()
Python getattr() FunctionThe python getattr() function returns the value of a named attribute of an object. If it is not found, it returns the default value. Python getattr() Function Example
Output: The age is: 22 The age is: 22 Python globals() FunctionThe python globals() function returns the dictionary of the current global symbol table. A Symbol table is defined as a data structure which contains all the necessary information about the program. It includes variable names, methods, classes, etc. Python globals() Function Example
Output: The age is: 22 Python hasattr() FunctionThe python any() function returns true if any item in an iterable is true, otherwise it returns False. Python hasattr() Function Example
Output: True False True False Python iter() FunctionThe python iter() function is used to return an iterator object. It creates an object which can be iterated one element at a time. Python iter() Function Example
Output: 1 2 3 4 5 Python len() FunctionThe python len() function is used to return the length (the number of items) of an object. Python len() Function Example
Output: 6 Python list()The python list() creates a list in python. Python list() Example
Output: [] ['a', 'b', 'c', 'd', 'e'] [1,2,3,4,5] [1,2,3,4,5] Python locals() FunctionThe python locals() method updates and returns the dictionary of the current local symbol table. A Symbol table is defined as a data structure which contains all the necessary information about the program. It includes variable names, methods, classes, etc. Python locals() Function Example
Output: localsAbsent: {}
localsPresent: {'present': True}
Python map() FunctionThe python map() function is used to return a list of results after applying a given function to each item of an iterable(list, tuple etc.). Python map() Function Example
Output: <map object at 0x7fb04a6bec18>
{8, 2, 4, 6}
Python memoryview() FunctionThe python memoryview() function returns a memoryview object of the given argument. Python memoryview () Function Example
Output: 65 b'AB' [65, 66, 67] Python object()The python object() returns an empty object. It is a base for all the classes and holds the built-in properties and methods which are default for all the classes. Python object() Example
Output: <class 'object'> ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] Python open() FunctionThe python open() function opens the file and returns a corresponding file object. Python open() Function Example
Output: Since the mode is omitted, the file is opened in 'r' mode; opens for reading. Python chr() FunctionPython chr() function is used to get a string representing a character which points to a Unicode code integer. For example, chr(97) returns the string 'a'. This function takes an integer argument and throws an error if it exceeds the specified range. The standard range of the argument is from 0 to 1,114,111. Python chr() Function Example
Output: ValueError: chr() arg not in range(0x110000) Python complex()Python complex() function is used to convert numbers or string into a complex number. This method takes two optional parameters and returns a complex number. The first parameter is called a real and second as imaginary parts. Python complex() Example
Output: (1.5+0j) (1.5+2.2j) Python delattr() FunctionPython delattr() function is used to delete an attribute from a class. It takes two parameters, first is an object of the class and second is an attribute which we want to delete. After deleting the attribute, it no longer available in the class and throws an error if try to call it using the class object. Python delattr() Function Example
Output: 101 Pranshu pranshu@abc.com AttributeError: course Python dir() FunctionPython dir() function returns the list of names in the current local scope. If the object on which method is called has a method named __dir__(), this method will be called and must return the list of attributes. It takes a single object type argument. Python dir() Function Example
Output: ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] Python divmod() FunctionPython divmod() function is used to get remainder and quotient of two numbers. This function takes two numeric arguments and returns a tuple. Both arguments are required and numeric Python divmod() Function Example
Output: (5, 0) Python enumerate() FunctionPython enumerate() function returns an enumerated object. It takes two parameters, first is a sequence of elements and the second is the start index of the sequence. We can get the elements in sequence either through a loop or next() method. Python enumerate() Function Example
Output: <enumerate object at 0x7ff641093d80> [(0, 1), (1, 2), (2, 3)] Python dict()Python dict() function is a constructor which creates a dictionary. Python dictionary provides three different constructors to create a dictionary:
Python dict() Example
Output: {}
{'a': 1, 'b': 2}
Python filter() FunctionPython filter() function is used to get filtered elements. This function takes two arguments, first is a function and the second is iterable. The filter function returns a sequence of those elements of iterable object for which function returns true value. The first argument can be none, if the function is not available and returns only elements that are true. Python filter() Function Example
Output: [6] Python hash() FunctionPython hash() function is used to get the hash value of an object. Python calculates the hash value by using the hash algorithm. The hash values are integers and used to compare dictionary keys during a dictionary lookup. We can hash only the types which are given below: Hashable types: * bool * int * long * float * string * Unicode * tuple * code object. Python hash() Function Example
Output: 21 461168601842737174 Python help() FunctionPython help() function is used to get help related to the object passed during the call. It takes an optional parameter and returns help information. If no argument is given, it shows the Python help console. It internally calls python's help function. Python help() Function Example
Output: Welcome to Python 3.5's help utility! Python min() FunctionPython min() function is used to get the smallest element from the collection. This function takes two arguments, first is a collection of elements and second is key, and returns the smallest element from the collection. Python min() Function Example
Output: 325 1000.25 Python set() FunctionIn python, a set is a built-in class, and this function is a constructor of this class. It is used to create a new set using elements passed during the call. It takes an iterable object as an argument and returns a new set object. Python set() Function Example
Output: set()
{'1', '2'}
{'a', 'n', 'v', 't', 'j', 'p', 'i', 'o'}
Python hex() FunctionPython hex() function is used to generate hex value of an integer argument. It takes an integer argument and returns an integer converted into a hexadecimal string. In case, we want to get a hexadecimal value of a float, then use float.hex() function. Python hex() Function Example
Output: 0x1 0x156 Python id() FunctionPython id() function returns the identity of an object. This is an integer which is guaranteed to be unique. This function takes an argument as an object and returns a unique integer number which represents identity. Two objects with non-overlapping lifetimes may have the same id() value. Python id() Function Example
Output: 139963782059696 139963805666864 139963781994504 Python setattr() FunctionPython setattr() function is used to set a value to the object's attribute. It takes three arguments, i.e., an object, a string, and an arbitrary value, and returns none. It is helpful when we want to add a new attribute to an object and set a value to it. Python setattr() Function Example
Output: 102 Sohan sohan@abc.com Python slice() FunctionPython slice() function is used to get a slice of elements from the collection of elements. Python provides two overloaded slice functions. The first function takes a single argument while the second function takes three arguments and returns a slice object. This slice object can be used to get a subsection of the collection. Python slice() Function Example
Output: slice(None, 5, None) slice(0, 5, 3) Python sorted() FunctionPython sorted() function is used to sort elements. By default, it sorts elements in an ascending order but can be sorted in descending also. It takes four arguments and returns a collection in sorted order. In the case of a dictionary, it sorts only keys, not values. Python sorted() Function Example
Output: ['a', 'a', 'i', 'j', 'n', 'o', 'p', 't', 't', 'v'] Python next() FunctionPython next() function is used to fetch next item from the collection. It takes two arguments, i.e., an iterator and a default value, and returns an element. This method calls on iterator and throws an error if no item is present. To avoid the error, we can set a default value. Python next() Function Example
Output: 256 32 82 Python input() FunctionPython input() function is used to get an input from the user. It prompts for the user input and reads a line. After reading data, it converts it into a string and returns it. It throws an error EOFError if EOF is read. Python input() Function Example
Output: Enter a value: 45 You entered: 45 Python int() FunctionPython int() function is used to get an integer value. It returns an expression converted into an integer number. If the argument is a floating-point, the conversion truncates the number. If the argument is outside the integer range, then it converts the number into a long type. If the number is not a number or if a base is given, the number must be a string. Python int() Function Example
Output: integer values : 10 10 10 Python isinstance() FunctionPython isinstance() function is used to check whether the given object is an instance of that class. If the object belongs to the class, it returns true. Otherwise returns False. It also returns true if the class is a subclass. The isinstance() function takes two arguments, i.e., object and classinfo, and then it returns either True or False. Python isinstance() function Example
Output: True False Python oct() FunctionPython oct() function is used to get an octal value of an integer number. This method takes an argument and returns an integer converted into an octal string. It throws an error TypeError, if argument type is other than an integer. Python oct() function Example
Output: Octal value of 10: 0o12 Python ord() FunctionThe python ord() function returns an integer representing Unicode code point for the given Unicode character. Python ord() function Example
Output: 56 82 38 Python pow() FunctionThe python pow() function is used to compute the power of a number. It returns x to the power of y. If the third argument(z) is given, it returns x to the power of y modulus z, i.e. (x, y) % z. Python pow() function Example
Output: 16 16 0.0625 0.0625 Python print() FunctionThe python print() function prints the given object to the screen or other standard output devices. Python print() function Example
Output: Python is programming language. x = 7 x = 7 = y Python range() FunctionThe python range() function returns an immutable sequence of numbers starting from 0 by default, increments by 1 (by default) and ends at a specified number. Python range() function Example
Output: [] [0, 1, 2, 3] [1, 2, 3, 4, 5, 6] Python reversed() FunctionThe python reversed() function returns the reversed iterator of the given sequence. Python reversed() function Example
Output: ['a', 'v', 'a', 'J'] ['a', 'v', 'a', 'J'] [11, 10, 9, 8] [5, 7, 2, 1] Python round() FunctionThe python round() function rounds off the digits of a number and returns the floating point number. Python round() Function Example
Output: 10 11 7 Python issubclass() FunctionThe python issubclass() function returns true if object argument(first argument) is a subclass of second class(second argument). Python issubclass() Function Example
Output: True False True True Python strThe python str() converts a specified value into a string. Python str() Function Example
Output: '4' Python tuple() FunctionThe python tuple() function is used to create a tuple object. Python tuple() Function Example
Output: t1= ()
t2= (1, 6, 9)
t1= ('J', 'a', 'v', 'a')
t1= (4, 5)
Python type()The python type() returns the type of the specified object if a single argument is passed to the type() built in function. If three arguments are passed, then it returns a new type object. Python type() Function Example
Output: <class 'list'> <class 'dict'> <class '__main__.Python'> Python vars() functionThe python vars() function returns the __dict__ attribute of the given object. Python vars() Function Example
Output: {'y': 9, 'x': 7}
Python zip() FunctionThe python zip() Function returns a zip object, which maps a similar index of multiple containers. It takes iterables (can be zero or more), makes it an iterator that aggregates the elements based on iterables passed, and returns an iterator of tuples. Python zip() Function Example
Output: []
{(5, 'five'), (4, 'four'), (6, 'six')}
Next TopicPython Lambda Functions
|
Python Lambda FunctionsPython Lambda function is known as the anonymous function that is defined without a name. Python allows us to not declare the function in the standard manner, i.e., by using the def keyword. Rather, the anonymous functions are declared by using the lambda keyword. However, Lambda functions can accept any number of arguments, but they can return only one value in the form of expression. The anonymous function contains a small piece of code. It simulates inline functions of C and C++, but it is not exactly an inline function. The syntax to define an anonymous function is given below. Syntax
It can accept any number of arguments and has only one expression. It is useful when the function objects are required. Consider the following example of the lambda function. Example 1
Output: <function <lambda> at 0x0000019E285D16A8> sum = 30 In the above example, we have defined the lambda a: a+10 anonymous function where a is an argument and a+10 is an expression. The given expression gets evaluated and returned the result. The above lambda function is same as the normal function.
Example 2Multiple arguments to Lambda function
Output: Enter the number:10 10 X 1 = 10 10 X 2 = 20 10 X 3 = 30 10 X 4 = 40 10 X 5 = 50 10 X 6 = 60 10 X 7 = 70 10 X 8 = 80 10 X 9 = 90 10 X 10 = 100 The lambda function is commonly used with Python built-in functions filter() function and map() function. Use lambda function with filter()The Python built-in filter() function accepts a function and a list as an argument. It provides an effective way to filter out all elements of the sequence. It returns the new sequence in which the function evaluates to True. Consider the following example where we filter out the only odd number from the given list.
Output: (37, 41, 123, 29) Using lambda function with map()The map() function in Python accepts a function and a list. It gives a new list which contains all modified items returned by the function for each item. Consider the following example of map() function.
Output: (100, 400, 900, 1600, 2500, 3600)
Next TopicPython Files Io
|
Python File HandlingTill now, we were taking the input from the console and writing it back to the console to interact with the user. Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console since the memory is volatile, it is impossible to recover the programmatically generated data again and again. The file handling plays an important role when the data needs to be stored permanently into the file. A file is a named location on disk to store related information. We can access the stored information (non-volatile) after the program termination. The file-handling implementation is slightly lengthy or complicated in the other programming language, but it is easier and shorter in Python. In Python, files are treated in two modes as text or binary. The file may be in the text or binary format, and each line of a file is ended with the special character. Hence, a file operation can be done in the following order.
Opening a filePython provides an open() function that accepts two arguments, file name and access mode in which the file is accessed. The function returns a file object which can be used to perform various operations like reading, writing, etc. Syntax:
The files can be accessed using various modes like read, write, or append. The following are the details about the access mode to open a file.
Let's look at the simple example to open a file named "file.txt" (stored in the same directory) in read mode and printing its content on the console. Example
Output: <class '_io.TextIOWrapper'> file is opened successfully In the above code, we have passed filename as a first argument and opened file in read mode as we mentioned r as the second argument. The fileptr holds the file object and if the file is opened successfully, it will execute the print statement The close() methodOnce all the operations are done on the file, we must close it through our Python script using the close() method. Any unwritten information gets destroyed once the close() method is called on a file object. We can perform any operation on the file externally using the file system which is the currently opened in Python; hence it is good practice to close the file once all the operations are done. The syntax to use the close() method is given below. Syntax
Consider the following example.
After closing the file, we cannot perform any operation in the file. The file needs to be properly closed. If any exception occurs while performing some operations in the file then the program terminates without closing the file. We should use the following method to overcome such type of problem.
The with statementThe with statement was introduced in python 2.5. The with statement is useful in the case of manipulating the files. It is used in the scenario where a pair of statements is to be executed with a block of code in between. The syntax to open a file using with the statement is given below.
The advantage of using with statement is that it provides the guarantee to close the file regardless of how the nested block exits. It is always suggestible to use the with statement in the case of files because, if the break, return, or exception occurs in the nested block of code then it automatically closes the file, we don't need to write the close() function. It doesn't let the file to corrupt. Consider the following example. Example
Writing the fileTo write some text to a file, we need to open the file using the open method with one of the following access modes. w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file. a: It will append the existing file. The file pointer is at the end of the file. It creates a new file if no file exists. Consider the following example. Example
Output: File2.txt Python is the modern-day language. It makes things so simple. It is the fastest growing programming language. Snapshot of the file2.txt
We have opened the file in w mode. The file1.txt file doesn't exist, it created a new file and we have written the content in the file using the write() function. Example 2
Output: Python is the modern day language. It makes things so simple. It is the fastest growing programing language Python has an easy syntax and user-friendly interaction. Snapshot of the file2.txt
We can see that the content of the file is modified. We have opened the file in a mode and it appended the content in the existing file2.txt. To read a file using the Python script, the Python provides the read() method. The read() method reads a string from the file. It can read the data in the text as well as a binary format. The syntax of the read() method is given below. Syntax:
Here, the count is the number of bytes to be read from the file starting from the beginning of the file. If the count is not specified, then it may read the content of the file until the end. Consider the following example. Example
Output: <class 'str'> Python is In the above code, we have read the content of file2.txt by using the read() function. We have passed count value as ten which means it will read the first ten characters from the file. If we use the following line, then it will print all content of the file.
Output: Python is the modern-day language. It makes things so simple. It is the fastest-growing programing language Python has easy an syntax and user-friendly interaction. Read file through for loopWe can read the file using for loop. Consider the following example.
Output: Python is the modern day language. It makes things so simple. Python has easy syntax and user-friendly interaction. Read Lines of the filePython facilitates to read the file line by line by using a function readline() method. The readline() method reads the lines of the file from the beginning, i.e., if we use the readline() method two times, then we can get the first two lines of the file. Consider the following example which contains a function readline() that reads the first line of our file "file2.txt" containing three lines. Consider the following example. Example 1: Reading lines using readline() function
Output: Python is the modern day language. It makes things so simple. We called the readline() function two times that's why it read two lines from the file. Python provides also the readlines() method which is used for the reading lines. It returns the list of the lines till the end of file(EOF) is reached. Example 2: Reading Lines Using readlines() function
Output: ['Python is the modern day language.\n', 'It makes things so simple.\n', 'Python has easy syntax and user-friendly interaction.'] Creating a new fileThe new file can be created by using one of the following access modes with the function open(). x: it creates a new file with the specified name. It causes an error a file exists with the same name. a: It creates a new file with the specified name if no such file exists. It appends the content to the file if the file already exists with the specified name. w: It creates a new file with the specified name if no such file exists. It overwrites the existing file. Consider the following example. Example 1
Output: <_io.TextIOWrapper name='file2.txt' mode='x' encoding='cp1252'> File created successfully File Pointer positionsPython provides the tell() method which is used to print the byte number at which the file pointer currently exists. Consider the following example.
Output: The filepointer is at byte : 0 After reading, the filepointer is at: 117 Modifying file pointer positionIn real-world applications, sometimes we need to change the file pointer location externally since we may need to read or write the content at various locations. For this purpose, the Python provides us the seek() method which enables us to modify the file pointer position externally. The syntax to use the seek() method is given below. Syntax:
The seek() method accepts two parameters: offset: It refers to the new position of the file pointer within the file. from: It indicates the reference position from where the bytes are to be moved. If it is set to 0, the beginning of the file is used as the reference position. If it is set to 1, the current position of the file pointer is used as the reference position. If it is set to 2, the end of the file pointer is used as the reference position. Consider the following example. Example
Output: The filepointer is at byte : 0 After reading, the filepointer is at: 10 Python OS moduleRenaming the fileThe Python os module enables interaction with the operating system. The os module provides the functions that are involved in file processing operations like renaming, deleting, etc. It provides us the rename() method to rename the specified file to a new name. The syntax to use the rename() method is given below. Syntax:
The first argument is the current file name and the second argument is the modified name. We can change the file name bypassing these two arguments. Example 1:
Output: The above code renamed current file2.txt to file3.txt Removing the fileThe os module provides the remove() method which is used to remove the specified file. The syntax to use the remove() method is given below.
Example 1
Creating the new directoryThe mkdir() method is used to create the directories in the current working directory. The syntax to create the new directory is given below. Syntax:
Example 1
The getcwd() methodThis method returns the current working directory. The syntax to use the getcwd() method is given below. Syntax
Example
Output: 'C:\\Users\\DEVANSH SHARMA' Changing the current working directory
The chdir() method is used to change the current working directory to a specified directory. The syntax to use the chdir() method is given below. Syntax
Example
Output: 'C:\\Users\\DEVANSH SHARMA\\Documents' Deleting directoryThe rmdir() method is used to delete the specified directory. The syntax to use the rmdir() method is given below. Syntax
Example 1
It will remove the specified directory. Writing Python output to the filesIn Python, there are the requirements to write the output of a Python script to a file. The check_call() method of module subprocess is used to execute a Python script and write the output of that script to a file. The following example contains two python scripts. The script file1.py executes the script file.py and writes its output to the text file output.txt. Example file.py
file.py
The file related methodsThe file object provides the following methods to manipulate the files on various operating systems.
Next TopicPython Modules
|
Python ModulesA python module can be defined as a python program file which contains a python code including python functions, class, or variables. In other words, we can say that our python code file saved with the extension (.py) is treated as the module. We may have a runnable code inside the python module. Modules in Python provides us the flexibility to organize the code in a logical way. To use the functionality of one module into another, we must have to import the specific module. ExampleIn this example, we will create a module named as file.py which contains a function func that contains a code to print some message on the console. Let's create the module named as file.py.
Here, we need to include this module into our main module to call the method displayMsg() defined in the module named file. Loading the module in our python codeWe need to load the module in our python code to use its functionality. Python provides two types of statements as defined below.
The import statementThe import statement is used to import all the functionality of one module into another. Here, we must notice that we can use the functionality of any python source file by importing that file as the module into another python source file. We can import multiple modules with a single import statement, but a module is loaded once regardless of the number of times, it has been imported into our file. The syntax to use the import statement is given below.
Hence, if we need to call the function displayMsg() defined in the file file.py, we have to import that file as a module into our module as shown in the example below. Example:
Output: Enter the name?John Hi John The from-import statementInstead of importing the whole module into the namespace, python provides the flexibility to import only the specific attributes of a module. This can be done by using from? import statement. The syntax to use the from-import statement is given below.
Consider the following module named as calculation which contains three functions as summation, multiplication, and divide. calculation.py:
Main.py:
Output: Enter the first number10 Enter the second number20 Sum = 30 The from...import statement is always better to use if we know the attributes to be imported from the module in advance. It doesn't let our code to be heavier. We can also import all the attributes from a module by using *. Consider the following syntax.
Renaming a modulePython provides us the flexibility to import some module with a specific name so that we can use this name to use that module in our python source file. The syntax to rename a module is given below.
Example
Output: Enter a?10 Enter b?20 Sum = 30 Using dir() functionThe dir() function returns a sorted list of names defined in the passed module. This list contains all the sub-modules, variables and functions defined in this module. Consider the following example. Example
Output: ['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner'] The reload() functionAs we have already stated that, a module is loaded once regardless of the number of times it is imported into the python source file. However, if you want to reload the already imported module to re-execute the top-level code, python provides us the reload() function. The syntax to use the reload() function is given below.
for example, to reload the module calculation defined in the previous example, we must use the following line of code.
Scope of variablesIn Python, variables are associated with two types of scopes. All the variables defined in a module contain the global scope unless or until it is defined within a function. All the variables defined inside a function contain a local scope that is limited to this function itself. We can not access a local variable globally. If two variables are defined with the same name with the two different scopes, i.e., local and global, then the priority will always be given to the local variable. Consider the following example. Example
Output: Hi David Python packages
The packages in python facilitate the developer with the application development environment by providing a hierarchical directory structure where a package contains sub-packages, modules, and sub-modules. The packages are used to categorize the application level code efficiently. Let's create a package named Employees in your home directory. Consider the following steps. 1. Create a directory with name Employees on path /home. 2. Create a python source file with name ITEmployees.py on the path /home/Employees. ITEmployees.py
3. Similarly, create one more python file with name BPOEmployees.py and create a function getBPONames(). 4. Now, the directory Employees which we have created in the first step contains two python modules. To make this directory a package, we need to include one more file here, that is __init__.py which contains the import statements of the modules defined in this directory. __init__.py
5. Now, the directory Employees has become the package containing two python modules. Here we must notice that we must have to create __init__.py inside a directory to convert this directory to a package. 6. To use the modules defined inside the package Employees, we must have to import this in our python source file. Let's create a simple python source file at our home directory (/home) which uses the modules defined in this package. Test.py
Output: ['John', 'David', 'Nick', 'Martin'] We can have sub-packages inside the packages. We can nest the packages up to any level depending upon the application requirements. The following image shows the directory structure of an application Library management system which contains three sub-packages as Admin, Librarian, and Student. The sub-packages contain the python modules.
Next TopicPython Exception Handling
|
Python ExceptionAn exception can be defined as an unusual condition in a program resulting in the interruption in the flow of the program. Whenever an exception occurs, the program stops the execution, and thus the further code is not executed. Therefore, an exception is the run-time errors that are unable to handle to Python script. An exception is a Python object that represents an error Python provides a way to handle the exception so that the code can be executed without any interruption. If we do not handle the exception, the interpreter doesn't execute all the code that exists after the exception. Python has many built-in exceptions that enable our program to run without interruption and give the output. These exceptions are given below: Common ExceptionsPython provides the number of built-in exceptions, but here we are describing the common standard exceptions. A list of common exceptions that can be thrown from a standard Python program is given below.
The problem without handling exceptionsAs we have already discussed, the exception is an abnormal condition that halts the execution of the program. Suppose we have two variables a and b, which take the input from the user and perform the division of these values. What if the user entered the zero as the denominator? It will interrupt the program execution and through a ZeroDivision exception. Let's see the following example. Example
Output: Enter a:10 Enter b:0 Traceback (most recent call last): File "exception-test.py", line 3, in <module> c = a/b; ZeroDivisionError: division by zero The above program is syntactically correct, but it through the error because of unusual input. That kind of programming may not be suitable or recommended for the projects because these projects are required uninterrupted execution. That's why an exception-handling plays an essential role in handling these unexpected exceptions. We can handle these exceptions in the following way. Exception handling in pythonThe try-expect statementIf the Python program contains suspicious code that may throw the exception, we must place that code in the try block. The try block must be followed with the except statement, which contains a block of code that will be executed if there is some exception in the try block.
Syntax
Consider the following example. Example 1
Output: Enter a:10 Enter b:0 Can't divide with zero We can also use the else statement with the try-except statement in which, we can place the code which will be executed in the scenario if no exception occurs in the try block. The syntax to use the else statement with the try-except statement is given below.
Consider the following program. Example 2
Output: Enter a:10 Enter b:0 can't divide by zero <class 'Exception'> The except statement with no exceptionPython provides the flexibility not to specify the name of exception with the exception statement. Consider the following example. Example
The except statement using with exception variableWe can use the exception variable with the except statement. It is used by using the as keyword. this object will return the cause of the exception. Consider the following example:
Output: Enter a:10 Enter b:0 can't divide by zero division by zero Points to remember
Example
Output: File not found Declaring Multiple ExceptionsThe Python allows us to declare the multiple exceptions with the except clause. Declaring multiple exceptions is useful in the cases where a try block throws multiple exceptions. The syntax is given below. Syntax
Consider the following example.
Output: Arithmetic Exception The try...finally blockPython provides the optional finally statement, which is used with the try statement. It is executed no matter what exception occurs and used to release the external resource. The finally block provides a guarantee of the execution. We can use the finally block with the try block in which we can pace the necessary code, which must be executed before the try statement throws an exception. The syntax to use the finally block is given below. Syntax
Example
Output: file closed Error Raising exceptionsAn exception can be raised forcefully by using the raise clause in Python. It is useful in in that scenario where we need to raise an exception to stop the execution of the program. For example, there is a program that requires 2GB memory for execution, and if the program tries to occupy 2GB of memory, then we can raise an exception to stop the execution of the program. The syntax to use the raise statement is given below. Syntax
Points to remember
Example
Output: Enter the age:17 The age is not valid Example 2 Raise the exception with message
Output: Enter a positive integer: -5 That is a negative number! Example 3
Output: Enter a:10 Enter b:0 The value of b can't be 0 Custom ExceptionThe Python allows us to create our exceptions that can be raised from the program and caught using the except clause. However, we suggest you read this section after visiting the Python object and classes. Consider the following example. Example
Output: Received error: 2000
Next TopicPython Date and Time
|
Python Date and timePython provides the datetime module work with real dates and times. In real-world applications, we need to work with the date and time. Python enables us to schedule our Python script to run at a particular timing. In Python, the date is not a data type, but we can work with the date objects by importing the module named with datetime, time, and calendar. In this section of the tutorial, we will discuss how to work with the date and time objects in Python. The datetime classes are classified in the six main classes.
TickIn Python, the time instants are counted since 12 AM, 1st January 1970. The function time() of the module time returns the total number of ticks spent since 12 AM, 1st January 1970. A tick can be seen as the smallest unit to measure the time. Consider the following example
Output: 1585928913.6519969 How to get the current time?The localtime() functions of the time module are used to get the current time tuple. Consider the following example. Example
Output: time.struct_time(tm_year=2020, tm_mon=4, tm_mday=3, tm_hour=21, tm_min=21, tm_sec=40, tm_wday=4, tm_yday=94, tm_isdst=0) Time tupleThe time is treated as the tuple of 9 numbers. Let's look at the members of the time tuple.
Getting formatted timeThe time can be formatted by using the asctime() function of the time module. It returns the formatted time for the time tuple being passed. Example
Output: Tue Dec 18 15:31:39 2018 Python sleep timeThe sleep() method of time module is used to stop the execution of the script for a given amount of time. The output will be delayed for the number of seconds provided as the float. Consider the following example. Example
Output: 0 1 2 3 4 The datetime ModuleThe datetime module enables us to create the custom date objects, perform various operations on dates like the comparison, etc. To work with dates as date objects, we have to import the datetime module into the python source code. Consider the following example to get the datetime object representation for the current time. Example
Output: 2020-04-04 13:18:35.252578 Creating date objectsWe can create the date objects bypassing the desired date in the datetime constructor for which the date objects are to be created. Consider the following example. Example
Output: 2020-04-04 00:00:00 We can also specify the time along with the date to create the datetime object. Consider the following example. Example
Output: 2020-04-04 01:26:40 In the above code, we have passed in datetime() function year, month, day, hour, minute, and millisecond attributes in a sequential manner. Comparison of two datesWe can compare two dates by using the comparison operators like >, >=, <, and <=. Consider the following example. Example
Output: fun hours The calendar modulePython provides a calendar object that contains various methods to work with the calendars. Consider the following example to print the calendar for the last month of 2018. Example
Output: March 2020 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Printing the calendar of whole yearThe prcal() method of calendar module is used to print the calendar of the entire year. The year of which the calendar is to be printed must be passed into this method. Example
Output:
Next TopicPython Regular Expressions
|
Python Regular ExpressionsThe regular expressions can be defined as the sequence of characters which are used to search for a pattern in a string. The module re provides the support to use regex in the python program. The re module throws an exception if there is some error while using the regular expression. The re module must be imported to use the regex functionalities in python.
Regex FunctionsThe following regex functions are used in the python.
Forming a regular expressionA regular expression can be formed by using the mix of meta-characters, special sequences, and sets. Meta-CharactersMetacharacter is a character with the specified meaning.
Special SequencesSpecial sequences are the sequences containing \ followed by one of the characters.
SetsA set is a group of characters given inside a pair of square brackets. It represents the special meaning.
The findall() functionThis method returns a list containing a list of all matches of a pattern within the string. It returns the patterns in the order they are found. If there are no matches, then an empty list is returned. Consider the following example. Example
Output: ['How', 'How'] The match objectThe match object contains the information about the search and the output. If there is no match found, the None object is returned. Example
Output: <class '_sre.SRE_Match'> <_sre.SRE_Match object; span=(0, 3), match='How'> The Match object methodsThere are the following methods associated with the Match object.
Example
Output: (0, 3) How How are you. How is everything
Next TopicPython OOPs Concepts
|
Python Sending Email using SMTPSimple Mail Transfer Protocol (SMTP) is used as a protocol to handle the email transfer using Python. It is used to route emails between email servers. It is an application layer protocol which allows to users to send mail to another. The receiver retrieves email using the protocols POP(Post Office Protocol) and IMAP(Internet Message Access Protocol).
When the server listens for the TCP connection from a client, it initiates a connection on port 587. Python provides a smtplib module, which defines an the SMTP client session object used to send emails to an internet machine. For this purpose, we have to import the smtplib module using the import statement.
The SMTP object is used for the email transfer. The following syntax is used to create the smtplib object.
It accepts the following parameters.
The sendmail() method of the SMTP object is used to send the mail to the desired machine. The syntax is given below.
Example
Sending email from gmailThere are cases where the emails are sent using the Gmail SMTP server. In this case, we can pass Gmail as the SMTP server instead of using the localhost with the port 587. Use the following syntax.
Here, we need to login to the Gmail account using Gmail user name and password. For this purpose, the smtplib provide the login() method, which accepts the username and password of the sender. This may make your Gmail ask you for access to less secure apps if you're using Gmail. You will need to turn this ON temporarily for this to work.
Consider the following example. Example
Sending HTML in emailWe can format the HTML in the message by specifying the MIME version, content-type, and character set to send the HTML. Consider the following example. Example
Next TopicPython read csv file
|
Python read csv fileCSV FileA csv stands for "comma separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a common format for data interchange. A csv file opens into the excel sheet, and the rows and columns data define the standard format. Python CSV Module FunctionsThe CSV module work is used to handle the CSV files to read/write and get data from specified columns. There are different types of CSV functions, which are as follows:
Reading CSV filesPython provides various functions to read csv file. We are describing few method of reading function.
In Python, the csv.reader() module is used to read the csv file. It takes each row of the file and makes a list of all the columns. We have taken a txt file named as python.txt that have default delimiter comma(,) with the following data:
Example
Output: Column names are name, department, birthday month Parker works in the Accounting department, and was born in November. Smith works in the IT department, and was born in October. Processed 3 lines. In the above code, we have opened 'python.csv' using the open() function. We used csv.reader() function to read the file, that returns an iterable reader object. The reader object have consisted the data and we iterated using for loop to print the content of each row Read a CSV into a DictionarWe can also use DictReader() function to read the csv file directly into a dictionary rather than deal with a list of individual string elements. Again, our input file, python.txt is as follows:
Example
Output: The Column names are as follows name, department, birthday month Parker works in the Accounting department, and was born in November. Smith works in the IT department, and was born in October. Processed 3 lines. Reading csv files with PandasThe Pandas is defined as an open-source library which is built on the top of the NumPy library. It provides fast analysis, data cleaning, and preparation of the data for the user. Reading the csv file into a pandas DataFrame is quick and straight forward. We don't need to write enough lines of code to open, analyze, and read the csv file in pandas and it stores the data in DataFrame. Here, we are taking a slightly more complicated file to read, called hrdata.csv, which contains data of company employees.
Example
In the above code, the three lines are enough to read the file, and only one of them is doing the actual work, i.e., pandas.read_csv() Output: Name Hire Date Salary Leaves Remaining 0 John Idle 03/15/14 50000.0 10 1 Smith Gilliam 06/01/15 65000.0 8 2 Parker Chapman 05/12/14 45000.0 10 3 Jones Palin 11/01/13 70000.0 3 4 Terry Gilliam 08/12/14 48000.0 7 5 Michael Palin 05/23/13 66000.0 8
Next TopicPython write csv file
|
Python Write CSV FileCSV FileA CSV stands for "comma-separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a standard format for data interchange. The CSV file opens into the excel sheet, and the rows and columns data define the standard format. Python CSV Module FunctionsThe CSV module work is to handle the CSV files to read/write and get data from specified columns. There are different types of CSV functions, which are as follows:
Writing CSV FilesWe can also write any new and existing CSV files in Python by using the csv.writer() module. It is similar to the csv.reader() module and also has two methods, i.e., writer function or the Dict Writer class. It presents two functions, i.e., writerow() and writerows(). The writerow() function only write one row, and the writerows() function write more than one row. It is defined as a construct that allows you to create, store, and re-use various formatting parameters. It supports several attributes; the most frequently used are:
Let's write the following data to a CSV File.
Example -
Output: Writing complete It returns the file named as 'Python.csv' that contains the following data:
Write a CSV into a DictionaryWe can also use the class DictWriter to write the CSV file directly into a dictionary. A file named as python.csv contains the following data: Parker, Accounting, November Smith, IT, October Example -
Output: emp_name,dept,birth_month Parker,Accounting,November Smith,IT,October Writing CSV Files Using PandasPandas is defined as an open source library which is built on the top of Numpy library. It provides fast analysis, data cleaning and preparation of the data for the user. It is as easy as reading the CSV file using pandas. You need to create the DataFrame, which is a two-dimensional, heterogeneous tabular data structure and consists of three main components- data, columns, and rows. Here, we take a slightly more complicated file to read, called hrdata.csv, which contains data of company employees.
Example -
Output: Employee, Hired, Salary, Sick Days John Idle, 2014-03-15, 50000.0,10 Smith Gilliam, 2015-06-01, 65000.0,8 Parker Chapman, 2014-05-12, 45000.0,10 Jones Palin, 2013-11-01, 70000.0,3 Terry Gilliam, 2014-08-12 , 48000.0,7 Michael Palin, 2013-05-23, 66000.0,8
Next TopicPython read excel file
|
Python read excel fileExcel is a spreadsheet application which is developed by Microsoft. It is an easily accessible tool to organize, analyze, and store the data in tables. It is widely used in many different applications all over the world. From Analysts to CEOs, various professionals use Excel for both quick stats and serious data crunching. Excel DocumentsAn Excel spreadsheet document is called a workbook which is saved in a file with .xlsx extension. The first row of the spreadsheet is mainly reserved for the header, while the first column identifies the sampling unit. Each workbook can contain multiple sheets that are also called a worksheets. A box at a particular column and row is called a cell, and each cell can include a number or text value. The grid of cells with data forms a sheet. The active sheet is defined as a sheet in which the user is currently viewing or last viewed before closing Excel. Reading from an Excel fileFirst, you need to write a command to install the xlrd module.
Creating a WorkbookA workbook contains all the data in the excel file. You can create a new workbook from scratch, or you can easily create a workbook from the excel file that already exists. Input File We have taken the snapshot of the workbook.
Code
Explanation: In the above example, firstly, we have imported the xlrd module and defined the location of the file. Then we have opened the workbook from the excel file that already exists. Reading from the PandasPandas is defined as an open-source library which is built on the top of the NumPy library. It provides fast analysis, data cleaning, and preparation of the data for the user and supports both xls and xlsx extensions from the URL. It is a python package which provides a beneficial data structure called a data frame. Example
Reading from the openpyxlFirst, we need to install an openpyxl module using pip from the command line.
After that, we need to import the module. We can also read data from the existing spreadsheet using openpyxl. It also allows the user to perform calculations and add content that was not part of the original dataset. Example
Output: My sheet title: Sheet To learn more about openpyxl, visit our complete tutorial Click Here. We have discussed essential detail in this tutorial.
Next TopicPython Write excel file
|
Python Write Excel FileThe Python write excel file is used to perform the multiple operations on a spreadsheet using the xlwt module. It is an ideal way to write data and format information to files with .xls extension. If you want to write data to any file and don't want to go through the trouble of doing everything by yourself, then you can use a for loop to automate the whole process a little bit. Write Excel File Using xlsxwriter ModuleWe can also write the excel file using the xlsxwriter module. It is defined as a Python module for writing the files in the XLSX file format. It can also be used to write text, numbers, and formulas to multiple worksheets. Also, it supports features such as charts, formatting, images, page setup, auto filters, conditional formatting, and many others. We need to use the following command to install xlsxwriter module:
Note- Throughout XlsxWriter, rows, and columns are zero-indexed. The first cell in a worksheet is listed as, A1 is (0,0), B1 is (0,1), A2 is (1,0), B2 is (1,1)......,and so on.Write Excel File Using openpyxl ModuleIt is defined as a package which is generally recommended if you want to read and write .xlsx, xlsm, xltx, and xltm files. You can check it by running type(wb). The load_workbook() function takes an argument and returns a workbook object, which represents the file. Make sure that you are in the same directory where your spreadsheet is located. Otherwise, you will get an error while importing. You can easily use a for loop with the help of the range() function to help you to print out the values of the rows that have values in column 2. If those particular cells are empty, you will get None. Writing data to Excel files with xlwtYou can use the xlwt package, apart from the XlsxWriter package to create the spreadsheets that contain your data. It is an alternative package for writing data, formatting information, etc. and ideal for writing the data and format information to files with .xls extension. It can perform multiple operations on the spreadsheet. It supports features such as formatting, images, charts, page setup, auto filters, conditional formatting, and many others. Pandas have excellent methods for reading all kinds of data from excel files. We can also import the results back to pandas. Writing Files with pyexcelYou can easily export your arrays back to a spreadsheet by using the save_as() function and pass the array and name of the destination file to the dest_file_name argument. It allows us to specify the delimiter and add dest_delimiter argument. You can pass the symbol that you want to use as a delimiter in-between " ". Code
Output:
Next TopicPython Assert Keyword
|
Python Assert KeywordPython assert keyword is defined as a debugging tool that tests a condition. The Assertions are mainly the assumption that asserts or state a fact confidently in the program. For example, while writing a division function, the divisor should not be zero, and you assert that the divisor is not equal to zero. It is merely a Boolean expression that has a condition or expression checks if the condition returns true or false. If it is true, the program does not do anything, and it moves to the next line of code. But if it is false, it raises an AssertionError exception with an optional error message. The main task of assertions is to inform the developers about unrecoverable errors in the program like "file not found", and it is right to say that assertions are internal self-checks for the program. It is the most essential for the testing or quality assurance in any application development area. The syntax of the assert keyword is given below. Syntax
Why Assertion is usedIt is a debugging tool, and its primary task is to check the condition. If it finds that the condition is true, it moves to the next line of code, and If not, then stops all its operations and throws an error. It points out the error in the code. Where Assertion in Python used
Example1This example shows the working of assert with the error message.
Output: The Average of scores2: 75.8 AssertionError: The List is empty. Explanation: In the above example, we have passed a non-empty list scores2 and an empty list scores1 to the avg() function. We received an output for scores2 list successfully, but after that, we got an error AssertionError: List is empty. The assert condition is satisfied by the scores2 list and lets the program continue to run. However, scores1 doesn't satisfy the condition and gives an AssertionError. Example2:This example shows the "Divide by 0 error" in the console.
Output: x / y value is : Runtime Exception :Traceback (most recent call last): File "main.py", line 6, in <module> assert y != 0, "Divide by 0 error" AssertionError: Divide by 0 error Explanation: In the above example, we have initialized an integer variable, i.e., x=7, y=0, and try to print the value of x/y as an output. The Python interpreter generated a Runtime Exception because of the assert keyword found the divisor as zero then displayed "Divide by 0 error" in the console.
Next TopicPython List Comprehension
|
Python List ComprehensionList Comprehension is defined as an elegant way to define, create a list in Python and consists of brackets that contains an expression followed by for clause. It is efficient in both computationally and in terms of coding space and time. SignatureThe list comprehension starts with '[' and ']'. [ expression for item in list if conditional ] Example
Output: ['P', 'y', 't', 'h', 'o', 'n'] Example
Output: ['P', 'y', 't', 'h', 'o', 'n'] Example
Output: OS
Next TopicPython Tutorial
|
Python Collection ModuleThe Python collection module is defined as a container that is used to store collections of data, for example - list, dict, set, and tuple, etc. It was introduced to improve the functionalities of the built-in collection containers. Python collection module was first introduced in its 2.4 release. There are different types of collection modules which are as follows: namedtuple()The Python namedtuple() function returns a tuple-like object with names for each position in the tuple. It was used to eliminate the problem of remembering the index of each field of a tuple object in ordinary tuples. Examples
Output: ('James', 24, 'M')
OrderedDict()The Python OrderedDict() is similar to a dictionary object where keys maintain the order of insertion. If we try to insert key again, the previous value will be overwritten for that key. Example
Output: A 10 C 12 B 11 D 13 defaultdict()The Python defaultdict() is defined as a dictionary-like object. It is a subclass of the built-in dict class. It provides all methods provided by dictionary but takes the first argument as a default data type. Example
Output: 0 Counter()The Python Counter is a subclass of dictionary object which helps to count hashable objects. Example
Output: 3 deque()The Python deque() is a double-ended queue which allows us to add and remove elements from both the ends. Example
Output: deque(['x', 'y', 'z']) Chainmap ObjectsA chainmap class is used to groups multiple dictionary together to create a single list. The linked dictionary stores in the list and it is public and can be accessed by the map attribute. Consider the following example. Example
Output: ['Name', 'Age', 'Roll_no' ] UserDict ObjectsThe UserDict behaves as a wrapper around the dictionary objects. The dictionary can be accessed as an attribute by using the UserDict object. It provides the easiness to work with the dictionary. It provides the following attribute. data - A real dictionary used to store the contents of the UserDict class. UserList ObjectsThe UserList behaves as a wrapper class around the list-objects. It is useful when we want to add new functionality to the lists. It provides the easiness to work with the dictionary. It provides the following attribute. data - A real list is used to store the contents of the User class. UserString ObjectsThe UserList behaves as a wrapper class around the list objects. The dictionary can be accessed as an attribute by using the UserString object. It provides the easiness to work with the dictionary. It provides the following attribute. data - A real str object is used to store the contents of the UserString class.
Next TopicPython math module
|
Python Math ModulePython math module is defined as the most famous mathematical functions, which includes trigonometric functions, representation functions, logarithmic functions, etc. Furthermore, it also defines two mathematical constants, i.e., Pie and Euler number, etc. Pie (n): It is a well-known mathematical constant and defined as the ratio of circumstance to the diameter of a circle. Its value is 3.141592653589793. Euler's number(e): It is defined as the base of the natural logarithmic, and its value is 2.718281828459045. There are different math modules which are given below: math.log()This method returns the natural logarithm of a given number. It is calculated to the base e. Example
Output: log(fabs(x), base) is : -6.698970004336019 math.log10()This method returns base 10 logarithm of the given number and called the standard logarithm. Example
Output: log10(x) is : 1.1139433523068367 math.exp()This method returns a floating-point number after raising e to the given number. Example
Output: The given number (x) is : 0.05 e^x (using exp() function) is : 0.05127109637602412 math.pow(x,y)This method returns the power of the x corresponding to the value of y. If value of x is negative or y is not integer value than it raises a ValueError. Example
Output: The power of number: 100.0 math.floor(x)This method returns the floor value of the x. It returns the less than or equal value to x. Example:
Output: The floor value is: 10 math.ceil(x)This method returns the ceil value of the x. It returns the greater than or equal value to x.
Output: The floor value is: 11 math.fabs(x)This method returns the absolute value of x.
Output: The absolute value is: 10.001 math.factorial()This method returns the factorial of the given number x. If x is not integral, it raises a ValueError. Example
Output: The factorial of number: 5040 math.modf(x)This method returns the fractional and integer parts of x. It carries the sign of x is float. Example
Output: The modf of number: (0.5, 44.0) Python provides the several math modules which can perform the complex task in single-line of code. In this tutorial, we have discussed a few important math modules.
Next TopicPython Tutorial
|
Python OS ModulePython OS module provides the facility to establish the interaction between the user and the operating system. It offers many useful OS functions that are used to perform OS-based tasks and get related information about operating system. The OS comes under Python's standard utility modules. This module offers a portable way of using operating system dependent functionality. The Python OS module lets us work with the files and directories.
There are some functions in the OS module which are given below: os.name()This function provides the name of the operating system module that it imports.
Currently, it registers 'posix', 'nt', 'os2', 'ce', 'java' and 'riscos'. Example
Output: nt os.mkdir()The os.mkdir() function is used to create new directory. Consider the following example.
It will create the new directory to the path in the string argument of the function in the D drive named folder newdir. os.getcwd()It returns the current working directory(CWD) of the file. Example
Output: C:\Users\Python\Desktop\ModuleOS os.chdir()The os module provides the chdir() function to change the current working directory.
Output: d:\\ os.rmdir()The rmdir() function removes the specified directory with an absolute or related path. First, we have to change the current working directory and remove the folder. Example
os.error()The os.error() function defines the OS level errors. It raises OSError in case of invalid or inaccessible file names and path etc. Example
Output: Problem reading: Python.txt os.popen()This function opens a file or from the command specified, and it returns a file object which is connected to a pipe. Example
Output: This is awesome os.close()This function closes the associated file with descriptor fr. Example
Output: Traceback (most recent call last): File "main.py", line 3, in file = open(fr, 'r') FileNotFoundError: [Errno 2] No such file or directory: 'Python1.txt' os.rename()A file or directory can be renamed by using the function os.rename(). A user can rename the file if it has privilege to change the file. Example
Output: Traceback (most recent call last): File "main.py", line 3, in os.rename(fd,'Python1.txt') FileNotFoundError: [Errno 2] No such file or directory: 'python.txt' -> 'Python1.txt' os.access()This function uses real uid/gid to test if the invoking user has access to the path. Example
Output: Exist path: False It access to read the file: False It access to write the file: False Check if path can be executed: False
Next TopicPython Random module
|
Python Random moduleThe Python random module functions depend on a pseudo-random number generator function random(), which generates the float number between 0.0 and 1.0. There are different types of functions used in a random module which is given below: random.random()This function generates a random float number between 0.0 and 1.0. random.randint()This function returns a random integer between the specified integers. random.choice()This function returns a randomly selected element from a non-empty sequence. Example
Output: The random number from list is : 84 random.shuffle()This function randomly reorders the elements in the list. random.randrange(beg,end,step)This function is used to generate a number within the range specified in its argument. It accepts three arguments, beginning number, last number, and step, which is used to skip a number in the range. Consider the following example.
Output: A random number from range is : 290 random.seed()This function is used to apply on the particular random number with the seed argument. It returns the mapper value. Consider the following example.
Output: The random number between 0 and 1 is : 0.4405576668981033
Next TopicPython statistics module
|
Python statistics modulePython statistics module provides the functions to mathematical statistics of numeric data. There are some popular statistical functions defined in this module. mean() functionThe mean() function is used to calculate the arithmetic mean of the numbers in the list. Example
Output: Mean is : 4.857142857142857 median() functionThe median() function is used to return the middle value of the numeric data in the list. Example
Output: Median of data-set is : 4.5 mode() functionThe mode() function returns the most common data that occurs in the list. Example
Output: Calculated Mode 2 stdev() functionThe stdev() function is used to calculate the standard deviation on a given sample which is available in the form of the list. Example
Output: Standard Deviation of sample is 1.5811388300841898 median_low()The median_low function is used to return the low median of numeric data in the list. Example
Output: Low median of the data-set is 5 median_high()The median_high function is used to return the high median of numeric data in the list. Example
Output: High median of the data-set is 6
Next TopicPython Tutorial
|
Python sys moduleThe python sys module provides functions and variables which are used to manipulate different parts of the Python Runtime Environment. It lets us access system-specific parameters and functions. import sys First, we have to import the sys module in our program before running any functions. sys.modules This function provides the name of the existing python modules which have been imported. sys.argv This function returns a list of command line arguments passed to a Python script. The name of the script is always the item at index 0, and the rest of the arguments are stored at subsequent indices. sys.base_exec_prefix This function provides an efficient way to the same value as exec_prefix. If not running a virtual environment, the value will remain the same. sys.base_prefix It is set up during Python startup, before site.py is run, to the same value as prefix. sys.byteorder It is an indication of the native byteorder that provides an efficient way to do something. sys.maxsize This function returns the largest integer of a variable. sys.path This function shows the PYTHONPATH set in the current system. It is an environment variable that is a search path for all the python modules. sys.stdin It is an object that contains the original values of stdin at the start of the program and used during finalization. It can restore the files. sys.getrefcount This function returns the reference count of an object. sys.exit This function is used to exit from either the Python console or command prompt, and also used to exit from the program in case of an exception. sys executable The value of this function is the absolute path to a Python interpreter. It is useful for knowing where python is installed on someone else machine. sys.platform This value of this function is used to identify the platform on which we are working.
Next TopicPython Tutorial
|
Python IDEsIDE stands for Integrated Development Environment is defined as a coding tool that helps to automate the process of editing, compiling, testing, etc. in an SDLC and it provides ease to the developer to run, write and debug the code. It is specially designed for software development that consists of several tools which is used for developing and testing the software. There are some Python IDEs which are as follows:
PyCharm
PyCharm was developed by the Jet Brains, and it is a cross-platform Integrated Development Environment (IDE) specially designed for python. It is the most widely used IDE and available in both paid version and free open-source as well. It saves ample time by taking care of routine tasks. It is a complete python IDE that is loaded with a rich set of features like auto code completion, quick project navigation, fast error checking and correction, remote development support, database accessibility, etc. Features
Spyder
Spyder is an open-source that has high recognition in the IDE market and most suitable for data science. The full name of Spyder is Scientific Python Development Environment. It supports all the significant platforms Linux, Windows, and MacOS X. It provides a set of features like localized code editor, document viewer, variable explorer, integrated console, etc. and supports no. of scientific modules like NumPy, SciPy, etc. Features
PyDev
PyDev is defined as one of the commonly used Python IDE, which is an external plugin for Eclipse. It is a natural choice of the Python developers that are coming from the Java background and very popular in the market as Python interpreter. Aleksandar Totic is famous for his contribution to Mosaic browser and worked on Pydev project during 2003-2004. Pydev has a feature which includes Django integration, automatic code completion, smart indents and block indents, etc. Features
Atom
Atom is developed by GitHub, which is initially started as an open-source, cross-platform. It is based on a framework, i.e., Electron which enables cross-platform desktop application using Chromium and Node.js and generally known as "Hackable Text Editor for the 21st century". Features
Wing
It is defined as a cross-platform IDE that is packed with necessary features and with decent development support. Its personal edition is free of cost. The pro version comes with a 30 days trial for the developers to try it out. It has several features that include auto-completion, syntax highlighting, indents, and debugging. Features
Jupyter Notebook
Jupyter is one of the most used IPython notebook editors that is used across the Data Science industry. It is a web application that is based on the server-client structure and allows you to create and manipulate notebook documents. It makes the best use of the fact that python is an interpreted language. Features
Thonny
Thonny is another IDE which is best suited for learning and teaching programming. It is a software developed at the University of Tartu and supports code completion and highlight syntax errors. Features
Rodeo
Rodeo is defined as one of the best IDE for python that is most widely used for data science projects like taking data and information from different resources. It supports cross-platform functionality and provides auto-completion of code. Features
Microsoft Visual Studio
Microsoft Visual Studio is an open-source code editor which was best suited for development and debugging of latest web and cloud projects. It has its own marketplace for extensions. Features
Eric Python
The Eric Python is an editor which is developed in Python itself and can be used for both professional and non-professional work. Features
Next TopicPython Tutorial
|
Python ArraysAn array is defined as a collection of items that are stored at contiguous memory locations. It is a container which can hold a fixed number of items, and these items should be of the same type. An array is popular in most programming languages like C/C++, JavaScript, etc. Array is an idea of storing multiple items of the same type together and it makes easier to calculate the position of each element by simply adding an offset to the base value. A combination of the arrays could save a lot of time by reducing the overall size of the code. It is used to store multiple values in single variable. If you have a list of items that are stored in their corresponding variables like this: car1 = "Lamborghini" car2 = "Bugatti" car3 = "Koenigsegg" If you want to loop through cars and find a specific one, you can use the array. The array can be handled in Python by a module named array. It is useful when we have to manipulate only specific data values. Following are the terms to understand the concept of an array: Element - Each item stored in an array is called an element. Index - The location of an element in an array has a numerical index, which is used to identify the position of the element. Array RepresentationAn array can be declared in various ways and different languages. The important points that should be considered are as follows:
Array operationsSome of the basic operations supported by an array are as follows:
The Array can be created in Python by importing the array module to the python program.
Accessing array elements We can access the array elements using the respective indices of those elements.
Output: First element: 2 Second element: 4 Second last element: 8 Explanation: In the above example, we have imported an array, defined a variable named as "a" that holds the elements of an array and print the elements by accessing elements through indices of an array. How to change or add elementsArrays are mutable, and their elements can be changed in a similar way like lists.
Output: array('i', [0, 2, 3, 5, 7, 10])
array('i' ,[0, 2, 4, 6, 8, 10])
Explanation: In the above example, we have imported an array and defined a variable named as "numbers" which holds the value of an array. If we want to change or add the elements in an array, we can do it by defining the particular index of an array where you want to change or add the elements. Why to use arrays in Python?A combination of arrays saves a lot of time. The array can reduce the overall size of the code. How to delete elements from an array?The elements can be deleted from an array using Python's del statement. If we want to delete any value from the array, we can do that by using the indices of a particular element.
Output: array('i', [10, 20, 40, 60])
Explanation: In the above example, we have imported an array and defined a variable named as "number" which stores the values of an array. Here, by using del statement, we are removing the third element [3] of the given array. Finding the length of an arrayThe length of an array is defined as the number of elements present in an array. It returns an integer value that is equal to the total number of the elements present in that array. Syntax
Array ConcatenationWe can easily concatenate any two arrays using the + symbol. Example
Output: Array c= array('d', [1.1, 2.1, 3.1, 2.6, 7.8, 3.7, 8.6])
Explanation In the above example, we have defined variables named as "a, b, c" that hold the values of an array. Example
Output: First element: 4 Second element: 7 Second last element: 22 Explanation: In the above example, first, we have imported an array and defined a variable named as "x" which holds the value of an array and then, we have printed the elements using the indices of an array.
Next TopicPython Tutorial
|
Python Command line argumentsThe Python supports the programs that can be run on the command line, complete with command line arguments. It is the input parameter that needs to be passed to the script when executing them. It means to interact with a command-line interface for the scripts. It provides a getopt module, in which command line arguments and options can be parsed. What is argument passing?The command ls is often used to get a summary of files and folders present in a particular directory. Why to use argparse?It means to communicate between the writer of a program and user which does not require going into the code and making changes to the script. It provides the ability to a user to enter into the command-line arguments. Access command line argumentsThe Python sys module provides access to command-line arguments via sys.argv. It solves the two purposes: Python sys moduleIt is a basic module that was shipped with Python distribution from the early days on. It is a similar approach as C library using argc/argv to access the arguments. The sys module implements command-line arguments in a simple list structure named sys.argv. Each list element represents a single argument. The first one -- sys.argv[0] -- is the name of Python script. The other list elements are sys.argv[1] to sys.argv[n]- are the command line arguments 2 to n. As a delimiter between arguments, space is used. Argument values that contain space in it have to be quoted, accordingly. It stores command-line arguments into a list; we can access it using sys.argv. This is very useful and a simple way to read command-line arguments as String.
Python getopt moduleThe Python getopt module extends the separation of the input string by parameter validation. Based on getopt C function, it allows both short and long options, including a value assignment. It is very similar to C getopt() function for parsing command line parameters. It is useful in parsing command line arguments where we want the user to enter some options. Code
Python argparse moduleIt offers a command-line interface with standardized output, whereas the former two solutions leave most of the work in your hands. argparse allows verification of fixed and optional arguments with a name checking as either UNIX or GNU style. It is the preferred way to parse command-line arguments. It provides a lot of option such as positional arguments, the default value for arguments, helps message, specifying the data type of argument etc. It makes it easy to write the user-friendly command-line interfaces. It automatically generates help and usage messages and issues errors when a user gives invalid arguments to the program. getopt.getopt method This method is used for parsing the command line options and parameter list. Syntax:
args- It is an argument list that needs to be parsed. options- A string of option letters that the script wants to recognize, with options that require an argument which should be followed by a colon(:). long_options(optional)- It must be a string with names of the long options, which should be supported.
Exception getopt.GetoptError This exception arises when an unrecognized option is found in the argument list or when any option requiring an argument is given none. The argument to the exception is a string that indicates the cause of the error. The attributes msg and opt to give the error message and related option. Code
Output: $ test.py -h usage: test.py -i <inputfile> -o <outputfile> $ test.py -i BMP -o usage: test.py -i <inputfile> -o <outputfile> $ test.py -i inputfile Input file is " inputfile Output file is " How to use command line arguments in python?
Docopt Docopt is used to create command line interfaces.
Fire Python Fire automatically generates a command line interface; you only need one line of code. Unlike the other modules, it works instantly. You don't need to define any arguments; all the methods are linked by default. To install it type:
Define or use a class:
You have the options matching to the class methods:
Next TopicPython Tutorial
|
Python magic methodPython magic method is defined as the special method which adds "magic" to a class. It starts and ends with double underscores, for example, _init_ or _str_. The built-in classes define many magic methods. The dir() function can be used to see the number of magic methods inherited by a class. It has two prefixes, and suffix underscores in the method name. It is most frequently used to define the overloaded behaviors of predefined operators. __init__ The _init_ method is called after the instance of the class has been created but before it returned to the caller. It is invoked without any call, when an instance of the class is created like constructors in other programming languages such as C++, Java, C#, PHP, etc. These methods are also known as initialize and are called after _new_. Its where you should initialize the instance variables. __str__ This function computes "informal" or a nicely printable string representation of an object and must return a string object. __repr__ This function is called by the repr() built-in function to compute the "official" string representation of an object and returns a machine-readable representation of a type. The goal of the _repr_ is to be unambiguous. __len__ This function should return the length of an object. __call__ We can make an object callable by adding the _call_ magic method, and it is another method that is not needed quite as often is _call_. If defined in a class, then that class can be called. But if it was a function, instance itself rather than modifying. __del__ Just as _init_, which is a constructor method, _del_ is like a destructor. If you have opened a file in _init _, then _del_ can close it. __bytes__ It offers to compute a byte-string representation of an object and should return a string object. __ge__ This method gets invoked when >= operator is used and returns True or False. __neg__ This function gets called for the unary operator. __ipow__ This function gets called on the exponents with arguments. e.g. a**=b. __le__ This function gets called on comparison using <= operator. _nonzero_ This function returns the Boolean value of the object. It gets invoked when the bool (self) function is called.
Next TopicPython Tutorial
|
Python Stack and QueueData structure organizes the storage in computers so that we can easily access and change data. Stacks and Queues are the earliest data structure defined in computer science. A simple Python list can act as a queue and stack as well. A queue follows FIFO rule (First In First Out) and used in programming for sorting. It is common for stacks and queues to be implemented with an array or linked list. StackA Stack is a data structure that follows the LIFO(Last In First Out) principle. To implement a stack, we need two simple operations:
Operations:
Characteristics:
Code
Output: ['Python', 'C', 'Android', 'Java', 'C++'] C++ ['Python', 'C', 'Android', 'Java'] Java ['Python', 'C', 'Android'] QueueA Queue follows the First-in-First-Out (FIFO) principle. It is opened from both the ends hence we can easily add elements to the back and can remove elements from the front. To implement a queue, we need two simple operations:
Operations on Queue
Characteristics
Note: The implementation of a queue is a little bit different. A queue follows the "First-In-First-Out". Time plays an important factor here. The Stack is fast because we insert and pop the elements from the end of the list, whereas in the queue, the insertion and pops are made from the beginning of the list, so it becomes slow. The cause of this time difference is due to the properties of the list, which is fast in the end operation but slow at the beginning operations because all other elements have to be shifted one by one.Code
Output: 9 6 7 4
Next TopicPython Tutorial
|
PySpark MLlibMachine Learning is a technique of data analysis that combines data with statistical tools to predict the output. This prediction is used by the various corporate industries to make a favorable decision. PySpark provides an API to work with the Machine learning called as mllib. PySpark's mllib supports various machine learning algorithms like classification, regression clustering, collaborative filtering, and dimensionality reduction as well as underlying optimization primitives. Various machine learning concepts are given below:
The pyspark.mllib library supports several classification methods such as binary classification, multiclass classification, and regression analysis. The object may belong to a different class. The objective of classification is to differentiate the data based on the information. Random Forest, Naive Bayes, Decision Tree are the most useful algorithms in classification.
Clustering is an unsupervised machine learning problem. It is used when you do not know how to classify the data; we require the algorithm to find patterns and classify the data accordingly. The popular clustering algorithms are the K-means clustering, Gaussian mixture model, Hierarchical clustering.
The fpm means frequent pattern matching, which is used for mining various items, itemsets, subsequences, or other substructure. It is mostly used in large-scale datasets.
The mllib.linalg utilities are used for linear algebra.
It is used to define the relevant data for making a recommendation. It is capable of predicting future preference and recommending the top items. For example, Online entertainment platform Netflix has a huge collection of movies, and sometimes people face difficulty in selecting the favorite items. This is the field where the recommendation plays an important role.
The regression is used to find the relationship and dependencies between variables. It finds the correlation between each feature of data and predicts the future values. The mllib package supports many other algorithms, classes, and functions. Here we will understand the basic concept of pyspak.mllib. MLlib FeaturesThe PySpark mllib is useful for iterative algorithms. The features are the following:
Let's have a look at the essential libraries of PySpark MLlib. MLlib Linear RegressionLinear regression is used to find the relationship and dependencies between variables. Consider the following code:
Output: +--------------------+--------------------+----------------+------------------+------------------+------------------+--------------------+-------------------+ | _c0| _c1| _c2| _c3| _c4| _c5| _c6| _c7| +--------------------+--------------------+----------------+------------------+------------------+------------------+--------------------+-------------------+ | Email| Address| Avatar|Avg Session Length| Time on App| Time on Website|Length of Membership|Yearly Amount Spent| |mstephenson@ferna...|835 Frank TunnelW...| Violet| 34.49726772511229| 12.65565114916675| 39.57766801952616| 4.0826206329529615| 587.9510539684005| | hduke@hotmail.com|4547 Archer Commo...| DarkGreen| 31.92627202636016|11.109460728682564|37.268958868297744| 2.66403418213262| 392.2049334443264| | pallen@yahoo.com|24645 Valerie Uni...| Bisque|33.000914755642675|11.330278057777512|37.110597442120856| 4.104543202376424| 487.54750486747207| |riverarebecca@gma...|1414 David Throug...| SaddleBrown| 34.30555662975554|13.717513665142507| 36.72128267790313| 3.120178782748092| 581.8523440352177| |mstephens@davidso...|14023 Rodriguez P...|MediumAquaMarine| 33.33067252364639|12.795188551078114| 37.53665330059473| 4.446308318351434| 599.4060920457634| |alvareznancy@luca...|645 Martha Park A...| FloralWhite|33.871037879341976|12.026925339755056| 34.47687762925054| 5.493507201364199| 637.102447915074| |katherine20@yahoo...|68388 Reyes Light...| DarkSlateBlue| 32.02159550138701|11.366348309710526| 36.68377615286961| 4.685017246570912| 521.5721747578274| | awatkins@yahoo.com|Unit 6538 Box 898...| Aqua|32.739142938380326| 12.35195897300293| 37.37335885854755| 4.4342734348999375| 549.9041461052942| |vchurch@walter-ma...|860 Lee KeyWest D...| Salmon| 33.98777289568564|13.386235275676436|37.534497341555735| 3.2734335777477144| 570.2004089636196| +--------------------+--------------------+----------------+------------------+------------------+------------------+--------------------+-------------------+ only showing top 10 rows In the following code, we are importing the VectorAssembler library to create a new column Independent feature:
Output: +------------------+ Independent Feature +------------------+ |34.49726772511229 | |31.92627202636016 | |33.000914755642675| |34.30555662975554 | |33.33067252364639 | |33.871037879341976| |32.02159550138701 | |32.739142938380326| |33.98777289568564 | +------------------+
Output: +--------------------++-------------------+ |Independent Feature | Yearly Amount Spent| +--------------------++-------------------+ |34.49726772511229 | 587.9510539684005 | |31.92627202636016 | 392.2049334443264 | |33.000914755642675 | 487.5475048674720 | |34.30555662975554 | 581.8523440352177 | |33.33067252364639 | 599.4060920457634 | |33.871037879341976 | 637.102447915074 | |32.02159550138701 | 521.5721747578274 | |32.739142938380326 | 549.9041461052942 | |33.98777289568564 | 570.2004089636196 | +--------------------++-------------------+ PySpark provides the LinearRegression() function to find the prediction of any given dataset. The syntax is given below:
MLlib K- Mean ClusterThe K- Mean cluster algorithm is one of the most popular and commonly used algorithms. It is used to cluster the data points into a predefined number of clusters. The below example is showing the use of MLlib K-Means Cluster library:
Parameters of PySpark MLlibThe few important parameters of PySpark MLlib are given below:
It is RDD of Ratings or (userID, productID, rating) tuple.
It represents Rank of the computed feature matrices (number of features).
It represents the number of iterations of ALS. (default: 5)
It is the Regularization parameter. (default : 0.01)
It is used to parallelize the computation of some number of blocks. Collaborative Filtering (mllib.recommendation)Collaborative filtering is a technique that is generally used for a recommender system. This technique is focused on filling the missing entries of a user-item. Association matrix spark.ml currently supports model-based collaborative filtering. In collaborative filtering, users and products are described by a small set of hidden factors that can be used to predict missing entries. Scaling of the regularization parameterThe regularization parameter regParam is scaled to solve least-squares problem. The least-square problem occurs when the number of ratings are user-generated in updating user factors, or the number of ratings the product received in updating product factors. Cold-start strategyThe ALS Model (Alternative Least Square Model) is used for prediction while making a common prediction problem. The problem encountered when user or items in the test dataset occurred that may not be present during training the model. It can occur in the two scenarios which are given below:
Next TopicPython Decorator
|
Python DecoratorDecorators are one of the most helpful and powerful tools of Python. These are used to modify the behavior of the function. Decorators provide the flexibility to wrap another function to expand the working of wrapped function, without permanently modifying it. In Decorators, functions are passed as an argument into another function and then called inside the wrapper function. It is also called meta programming where a part of the program attempts to change another part of program at compile time. Before understanding the Decorator, we need to know some important concepts of Python. What are the functions in Python?Python has the most interesting feature that everything is treated as an object even classes or any variable we define in Python is also assumed as an object. Functions are first-class objects in the Python because they can reference to, passed to a variable and returned from other functions as well. The example is given below:
Output: Hii Hii In the above program, when we run the code it give the same output for both functions. The func2 referred to function func1 and act as function. We need to understand the following concept of the function:
Inner FunctionPython provides the facility to define the function inside another function. These types of functions are called inner functions. Consider the following example:
Output: We are in first function This is first child function This is second child function In the above program, it doesn't matter how the child functions are declared. The execution of the child function makes effect on the output. These child functions are locally bounded with the func() so they cannot be called separately. A function that accepts other function as an argument is also called higher order function. Consider the following example:
Output: 9 21 In the above program, we have passed the sub() function and add() function as argument in operator() function. A function can return another function. Consider the below example:
Output: Hello In the above program, the hi() function is nested inside the hello() function. It will return each time we call hi(). Decorating functions with parametersLet's have an example to understand the parameterized decorator function:
Output: 2.0 Syntactic DecoratorIn the above program, we have decorated out_div() that is little bit bulky. Instead of using above method, Python allows to use decorator in easy way with @symbol. Sometimes it is called "pie" syntax.
Output: 2.0 Reusing DecoratorWe can reuse the decorator as well by recalling that decorator function. Let's make the decorator to its own module that can be used in many other functions. Creating a file called mod_decorator.py with the following code:
We can import mod_decorator.py in other file.
Output: Hello There Hello There Python Decorator with ArgumentWe want to pass some arguments in function. Let's do it in following code:
Output: TypeError: display() missing 1 required positional argument: 'name' As we can see that, the function didn't accept the argument. Running this code raises an error. We can fix this error by using *args and **kwargs in the inner wrapper function. Modifying the decorator.py as follows:
Now wrapper_function() can accept any number of argument and pass them on the function.
Output: Hello John Hello John Returning Values from Decorated FunctionsWe can control the return type of the decorated function. The example is given below:
Output: We are created greeting We are created greeting Fancy DecoratorsLet's understand the fancy decorators by the following topic: Class DecoratorsPython provides two ways to decorate a class. Firstly, we can decorate the method inside a class; there are built-in decorators like @classmethod, @staticmethod and @property in Python. The @classmethod and @staticmethod define methods inside class that is not connected to any other instance of a class. The @property is generally used to modify the getters and setters of a class attributes. Let’s understand it by the following example: Example: 1- @property decorator - By using it, we can use the class function as an attribute. Consider the following code:
Output: Name: John Grade: B John got grade B Example:2 - @staticmethod decorator- The @staticmethod is used to define a static method in the class. It is called by using the class name as well as instance of the class. Consider the following code:
Output: Hello Peter Hello Peter Singleton ClassA singleton class only has one instance. There are many singletons in Python including True, None, etc. Nesting DecoratorsWe can use multiple decorators by using them on top of each other. Let's consider the following example:
In the above code, we have used the nested decorator by stacking them onto one another. Decorator with ArgumentsIt is always useful to pass arguments in a decorator. The decorator can be executed several times according to the given value of the argument. Let us consider the following example:
Output: JavatPoint JavatPoint JavatPoint JavatPoint JavatPoint In the above example, @repeat refers to a function object that can be called in another function. The @repeat(num = 5) will return a function which acts as a decorator. The above code may look complex but it is the most commonly used decorator pattern where we have used one additional def that handles the arguments to the decorator. Note: Decorator with argument is not frequently used in programming, but it provides flexibility. We can use it with or without argument.Stateful DecoratorsStateful decorators are used to keep track of the decorator state. Let us consider the example where we are creating a decorator that counts how many times the function has been called.
Output: Call 1 of 'say_hello' Say Hello Call 2 of 'say_hello' Say Hello In the above program, the state represented the number of calls of the function stored in .num_calls on the wrapper function. When we call say_hello() it will display the number of the call of the function. Classes as DecoratorsThe classes are the best way to maintain state. In this section, we will learn how to use a class as a decorator. Here we will create a class that contains __init__() and take func as an argument. The class needs to be callable so that it can stand in for the decorated function. To making a class callable, we implement the special __call__() method.
Output: Call 1 of 'say_hello' Say Hello Call 2 of 'say_hello' Say Hello Call 3 of 'say_hello' Say Hello The __init__() method stores a reference to the function and can do any other required initialization.
Next TopicPython Generators
|
Python GeneratorsWhat is Python Generator?Python Generators are the functions that return the traversal object and used to create iterators. It traverses the entire items at once. The generator can also be an expression in which syntax is similar to the list comprehension in Python. There is a lot of complexity in creating iteration in Python; we need to implement __iter__() and __next__() method to keep track of internal states. It is a lengthy process to create iterators. That's why the generator plays an essential role in simplifying this process. If there is no value found in iteration, it raises StopIteration exception. How to Create Generator function in Python?It is quite simple to create a generator in Python. It is similar to the normal function defined by the def keyword and uses a yield keyword instead of return. Or we can say that if the body of any function contains a yield statement, it automatically becomes a generator function. Consider the following example:
Output: 0 2 4 6 8 yield vs. returnThe yield statement is responsible for controlling the flow of the generator function. It pauses the function execution by saving all states and yielded to the caller. Later it resumes execution when a successive function is called. We can use the multiple yield statement in the generator function. The return statement returns a value and terminates the whole function and only one return statement can be used in the function. Using multiple yield Statement We can use the multiple yield statement in the generator function. Consider the following example.
Output: First String Second string Third String Difference between Generator function and Normal function
Generator ExpressionWe can easily create a generator expression without using user-defined function. It is the same as the lambda function which creates an anonymous function; the generator's expressions create an anonymous generator function. The representation of generator expression is similar to the Python list comprehension. The only difference is that square bracket is replaced by round parentheses. The list comprehension calculates the entire list, whereas the generator expression calculates one item at a time. Consider the following example:
Output: <generator object <genexpr> at 0x01BA3CD8> [1, 8, 27, 64, 125, 216, 343] In the above program, list comprehension has returned the list of cube of elements whereas generator expression has returned the reference of calculated value. Instead of applying a for loop, we can also call next() on the generator object. Let's consider another example:
Output: 1 8 27 64 Note:- When we call the next(), Python calls __next__() on the function in which we have passed it as a parameter.In the above program, we have used the next() function, which returned the next item of the list. Example: Write a program to print the table of the given number using the generator.
Output: 15 30 45 60 75 90 105 120 135 150 In the above example, a generator function is iterating using for loop. Advantages of GeneratorsThere are various advantages of Generators. Few of them are given below: 1. Easy to implementGenerators are easy to implement as compared to the iterator. In iterator, we have to implement __iter__() and __next__() function. 2. Memory efficientGenerators are memory efficient for a large number of sequences. The normal function returns a sequence of the list which creates an entire sequence in memory before returning the result, but the generator function calculates the value and pause their execution. It resumes for successive call. An infinite sequence generator is a great example of memory optimization. Let's discuss it in the below example by using sys.getsizeof() function.
Output: Memory in Bytes: 4508 Memory in Bytes: 56 We can observe from the above output that list comprehension is using 4508 bytes of memory, whereas generator expression is using 56 bytes of memory. It means that generator objects are much efficient than the list compression. 3. Pipelining with GeneratorsData Pipeline provides the facility to process large datasets or stream of data without using extra computer memory. Suppose we have a log file from a famous restaurant. The log file has a column (4th column) that keeps track of the number of burgers sold every hour and we want to sum it to find the total number of burgers sold in 4 years. In that scenario, the generator can generate a pipeline with a series of operations. Below is the code for it:
4. Generate Infinite Sequence The generator can produce infinite items. Infinite sequences cannot be contained within the memory and since generators produce only one item at a time, consider the following example:
Output: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ......... .......... 315 316 317 Traceback (most recent call last): File "C:\Users\DEVANSH SHARMA\Desktop\generator.py", line 33, in <module> print(i) KeyboardInterrupt In this tutorial, we have learned about the Python Generators.
Next TopicWeb Scraping Using Python
|
Web Scraping Using PythonWhat is Web Scraping?Web Scraping is a technique to extract a large amount of data from several websites. The term "scraping" refers to obtaining the information from another source (webpages) and saving it into a local file. For example: Suppose you are working on a project called "Phone comparing website," where you require the price of mobile phones, ratings, and model names to make comparisons between the different mobile phones. If you collect these details by checking various sites, it will take much time. In that case, web scrapping plays an important role where by writing a few lines of code you can get the desired results.
Web Scrapping extracts the data from websites in the unstructured format. It helps to collect these unstructured data and convert it in a structured form. Startups prefer web scrapping because it is a cheap and effective way to get a large amount of data without any partnership with the data selling company. Is Web Scrapping legal?Here the question arises whether the web scrapping is legal or not. The answer is that some sites allow it when used legally. Web scraping is just a tool you can use it in the right way or wrong way. Web scrapping is illegal if someone tries to scrap the nonpublic data. Nonpublic data is not reachable to everyone; if you try to extract such data then it is a violation of the legal term. There are several tools available to scrap data from websites, such as:
Why Web Scrapping?
As we have discussed above, web scrapping is used to extract the data from websites. But we should know how to use that raw data. That raw data can be used in various fields. Let's have a look at the usage of web scrapping:
It is widely used to collect data from several online shopping sites and compare the prices of products and make profitable pricing decisions. Price monitoring using web scrapped data gives the ability to the companies to know the market condition and facilitate dynamic pricing. It ensures the companies they always outrank others.
eb Scrapping is perfectly appropriate for market trend analysis. It is gaining insights into a particular market. The large organization requires a great deal of data, and web scrapping provides the data with a guaranteed level of reliability and accuracy.
Many companies use personals e-mail data for email marketing. They can target the specific audience for their marketing.
A single news cycle can create an outstanding effect or a genuine threat to your business. If your company depends on the news analysis of an organization, it frequently appears in the news. So web scraping provides the ultimate solution to monitoring and parsing the most critical stories. News articles and social media platform can directly influence the stock market.
Web Scrapping plays an essential role in extracting data from social media websites such as Twitter, Facebook, and Instagram, to find the trending topics.
The large set of data such as general information, statistics, and temperature is scrapped from websites, which is analyzed and used to carry out surveys or research and development. Why use Python for Web Scrapping?There are other popular programming languages, but why we choose the Python over other programming languages for web scraping? Below we are describing a list of Python's features that make the most useful programming language for web scrapping.
In Python, we don't need to define data types for variables; we can directly use the variable wherever it requires. It saves time and makes a task faster. Python defines its classes to identify the data type of variable.
Python comes with an extensive range of libraries such as NumPy, Matplotlib, Pandas, Scipy, etc., that provide flexibility to work with various purposes. It is suited for almost every emerging field and also for web scrapping for extracting data and do manipulation.
The purpose of the web scrapping is to save time. But what if you spend more time in writing the code? That's why we use Python, as it can perform a task in a few lines of code.
Python is open-source, which means it is freely available for everyone. It has one of the biggest communities across the world where you can seek help if you get stuck anywhere in Python code. The basics of web scrapingThe web scrapping consists of two parts: a web crawler and a web scraper. In simple words, the web crawler is a horse, and the scrapper is the chariot. The crawler leads the scrapper and extracts the requested data. Let's understand about these two components of web scrapping:
How does Web Scrapping work?These are the following steps to perform web scraping. Let's understand the working of web scraping. Step -1: Find the URL that you want to scrape First, you should understand the requirement of data according to your project. A webpage or website contains a large amount of information. That's why scrap only relevant information. In simple words, the developer should be familiar with the data requirement. Step - 2: Inspecting the Page The data is extracted in raw HTML format, which must be carefully parsed and reduce the noise from the raw data. In some cases, data can be simple as name and address or as complex as high dimensional weather and stock market data. Step - 3: Write the code Write a code to extract the information, provide relevant information, and run the code. Step - 4: Store the data in the file Store that information in required csv, xml, JSON file format. Getting Started with Web ScrappingPython has a vast collection of libraries and also provides a very useful library for web scrapping. Let's understand the required library for Python. Library used for web scrapping
Note - It is good to use the PyCharm IDE.
Pandas library is used for data manipulation and analysis. It is used to extract the data and store it in the desired format.
Let's understand the BeautifulSoup library in detail. Installation of BeautifulSoup You can install BeautifulSoup by typing the following command:
Installing a parser BeautifulSoup supports HTML parser and several third-party Python parsers. You can install any of them according to your dependency. The list of BeautifulSoup's parsers is the following:
We recommend you to install html5lib parser because it is much suitable for the newer version of Python, or you can install lxml parser. Type the following command in your terminal:
BeautifulSoup is used to transform a complex HTML document into a complex tree of Python objects. But there are a few essential types object which are mostly used:
A Tag object corresponds to an XML or HTML original document.
Output: <class "bs4.element.Tag"> Tag contains lot of attributes and methods, but most important features of a tag are name and attribute.
Every tag has a name, accessible as .name:
A tag may have any number of attributes. The tag <b id = "boldest"> has an attribute "id" whose value is "boldest". We can access a tag's attributes by treating the tag as dictionary.
We can add, remove, and modify a tag's attributes. It can be done by using tag as dictionary.
In HTML5, there are some attributes that can have multiple values. The class (consists more than one css) is the most common multivalued attributes. Other attributes are rel, rev, accept-charset, headers, and accesskey.
A string in BeautifulSoup refers text within a tag. BeautifulSoup uses the NavigableString class to contain these bits of text.
A string is immutable means it can't be edited. But it can be replaced with another string using replace_with().
In some cases, if you want to use a NavigableString outside the BeautifulSoup, the unicode() helps it to turn into normal Python Unicode string.
The BeautifulSoup object represents the complete parsed document as a whole. In many cases, we can use it as a Tag object. It means it supports most of the methods described in navigating the tree and searching the tree.
Output: ?xml version="1.0" encoding="utf-8"?> # <document><content/><footer>Here's the footer</footer></document> Web Scrapping Example:Let's take an example to understand the scrapping practically by extracting the data from the webpage and inspecting the whole page. First, open your favorite page on Wikipedia and inspect the whole page, and before extracting data from the webpage, you should ensure your requirement. Consider the following code:
Output: The object type <class 'requests.models.Response'> Convert the object into: <class 'bs4.BeautifulSoup'> In the following lines of code, we are extracting all headings of a webpage by class name. Here front-end knowledge plays an essential role in inspecting the webpage.
Output: Overview,Machine learning tasks,History and relationships to other fields,Relation to data mining,Relation to optimization,Relation to statistics, Theory,Approaches,Types of learning algorithms,Supervised learning,Unsupervised learning,Reinforcement learning,Self-learning,Feature learning,Sparse dictionary learning,Anomaly detection,Association rules,Models,Artificial neural networks,Decision trees,Support vector machines,Regression analysis,Bayesian networks,Genetic algorithms,Training models,Federated learning,Applications,Limitations,Bias,Model assessments,Ethics,Software,Free and open-source software,Proprietary software with free and open-source editions,Proprietary software,Journals,Conferences,See also,References,Further reading,External links, In the above code, we imported the bs4 and requested the library. In the third line, we created a res object to send a request to the webpage. As you can observe that we have extracted all heading from the webpage.
Webpage of Wikipedia Learning Let's understand another example; we will make a GET request to the URL and create a parse Tree object (soup) with the use of BeautifulSoup and Python built-in "html5lib" parser. Here we will scrap the webpage of given link (https://www.javatpoint.com/). Consider the following code:
The above code will display the all html code of javatpoint homepage. Using the BeautifulSoup object, i.e. soup, we can collect the required data table. Let's print some interesting information using the soup object:
Output: It will give an output as follow: <title>Tutorials List - Javatpoint</title>
Output: It will give an output as follow: Tutorials List - Javatpoint
Output: It will print all links along with its attributes. Here we display a few of them: href is: https://www.facebook.com/javatpoint Inner Text is: The title is: None href is: https://twitter.com/pagejavatpoint Inner Text is: The title is: None href is: https://www.youtube.com/channel/UCUnYvQVCrJoFWZhKK3O2xLg Inner Text is: The title is: None href is: https://javatpoint.blogspot.com Inner Text is: Learn Java Title is: None href is: https://www.javatpoint.com/java-tutorial Inner Text is: Learn Data Structures Title is: None href is: https://www.javatpoint.com/data-structure-tutorial Inner Text is: Learn C Programming Title is: None href is: https://www.javatpoint.com/c-programming-language-tutorial Inner Text is: Learn C++ Tutorial Demo: Scraping Data from Flipkart WebsiteIn this example, we will scrap the mobile phone prices, ratings, and model name from Flipkart, which is one of the popular e-commerce websites. Following are the prerequisites to accomplish this task: Prerequisites:
Step - 1: Find the desired URL to scrap The initial step is to find the URL that you want to scrap. Here we are extracting mobile phone details from the flipkart. The URL of this page is https://www.flipkart.com/search?q=iphones&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off. Step -2: Inspecting the page It is necessary to inspect the page carefully because the data is usually contained within the tags. So we need to inspect to select the desired tag. To inspect the page, right-click on the element and click "inspect". Step - 3: Find the data for extracting Extract the Price, Name, and Rating, which are contained in the "div" tag, respectively. Step - 4: Write the Code
Output:
We scrapped the details of the iPhone and saved those details in the CSV file as you can see in the output. In the above code, we put a comment on the few lines of code for testing purpose. You can remove those comments and observe the output. In this tutorial, we have discussed all basic concepts of web scrapping and described the sample scrapping from the leading online ecommerce site flipkart.
Next TopicPython JSON
|
Python JSONJSON stands for JavaScript Object Notation, which is a widely used data format for data interchange on the web. JSON is the ideal format for organizing data between a client and a server. Its syntax is similar to the JavaScript programming language. The main objective of JSON is to transmit the data between the client and the web server. It is easy to learn and the most effective way to interchange the data. It can be used with various programming languages such as Python, Perl, Java, etc. JSON mainly supports 6 types of data type In JavaScript:
JSON is built on the two structures:
JSON data representation is similar to the Python dictionary. Below is an example of JSON data:
Working with Python JSONPython provides a module called json. Python supports standard library marshal and pickle module, and JSON API behaves similarly as these library. Python natively supports JSON features. The encoding of JSON data is called Serialization. Serialization is a technique where data transforms in the series of bytes and transmitted across the network. The deserialization is the reverse process of decoding the data that is converted into the JSON format. This module includes many built-in functions. Let's have a look at these functions:
Output: ['JSONDecodeError', 'JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_default_decoder', '_default_encoder', 'codecs', 'decoder', 'detect_encoding', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner'] In this section, we will learn the following methods:
Serializing JSONSerialization is the technique to convert the Python objects to JSON. Sometimes, computer need to process lots of information so it is good to store that information into the file. We can store JSON data into file using JSON function. The json module provides the dump() and dumps() method that are used to transform Python object. Python objects are converted into the following JSON objects. The list is given below:
Writing JSON Data into File Python provides a dump() function to transmit(encode) data in JSON format. It accepts two positional arguments, first is the data object to be serialized and second is the file-like object to which the bytes needs to be written. Let's consider the simple serialization example:
Output: {"Name" : "Peter", "Roll_no" : "0090014" , "Grade" : "A", "Age" : 20, "Subject" : ["Computer Graphics", "Discrete Mathematics", "Data Structure"] }
In the above program, we have opened a file named data.json in writing mode. We opened this file in write mode because if the file doesn't exist, it will be created. The json.dump() method transforms dictionary into JSON string.
The dumps() function is used to store serialized data in the Python file. It accepts only one argument that is Python data for serialization. The file-like argument is not used because we aren't not writing data to disk. Let's consider the following example:
Output: {"Name": "Peter", "Roll_no": "0090014", "Grade": "A", "Age": 20}
JSON supports primitive data types, such as strings and numbers, as well as nested list, tuples and objects.
Output: ["Welcome", "to", "javaTpoint"] ["Welcome", "to", "javaTpoint"] "Hello" 1234 23.572 true false null Deserializing JSONDeserialization is the process to decode the JSON data into the Python objects. The json module provides two methods load() and loads(), which are used to convert JSON data in actual Python object form. The list is given below:
The above table shows the inverse of the serialized table but technically it is not a perfect conversion of the JSON data. It means that if we encode the object and decode it again after sometime; we may not get the same object back. Let's take real-life example, one person translates something into Chinese and another person translates back into English, and that may not be exactly translated. Consider the simple example:
Output: <class 'tuple'> <class 'list'>
The load() function is used to deserialize the JSON data to Python object from the file. Consider the following example:
Output: {'Name': 'Peter', 'Roll_no': '0090014', 'Grade': 'A', 'Age': 20}
In the above program, we have encoded Python object in the file using dump() function. After that we read JSON file using load() function, where we have passed read_file as an argument. The json module also provides loads() function, which is used to convert JSON data to Python object. It is quite similar to the load() function. Consider the following example:
Output: ['Mathew', 'Peter', [10, 32.9, 80], {'Name': 'Tokyo'}]
json.load() vs json.loads()The json.load() function is used to load JSON file, whereas json.loads() function is used to load string. json.dump() vs json.dumps()The json.dump() function is used when we want to serialize the Python objects into JSON file and json.dumps() function is used to convert JSON data as a string for parsing and printing. Python Pretty Print JSONSometimes we need to analyze and debug a large amount of JSON data. It can be done by passing additional arguments indent and sort_keys in json.dumps() and json.dump() methods. Note: Both dump() and dumps() functions accept indent and short_keys arguments.Consider the following example:
Output: {
"Age": 23,
"City": "English",
"Name": "Andrew",
"Number": 90014,
"Subject": [
"Data Structure",
"Computer Graphics",
"Discrete mathematics"
]
}
In the above code, we have provided the 5 spaces to the indent argument and the keys are sorted in ascending order. The default value of indent is None and the default value of sort_key is False. Encoding and DecodingEncoding is the technique for transforming the text or values into an encrypted form. Encrypted data can only be used by the preferred user by decoding it. Encoding is also known as serialization and decoding is also called deserialization. Encoding and decoding are done for JSON(object) format. Python provides a popular package for such operations. We can install it on Windows by the following command:
Encoding - The demjson package provides encode() function that is used to convert the Python object into a JSON string representation. The syntax is given below:
Example:1 - Encoding using demjson package
Output: [{"Age":20,"Name":"Peter","Subject":"Electronics"}]
Decoding-The demjson module provides decode() function, which is used to convert JSON object into Python format type. The syntax is given below:
Output: ['Peter', 'Smith', 'Ricky', 'Hayden'] In this tutorial, we have learned about the Python JSON. JSON is the most effective way to transmit data between the client and the web server.
Next TopicPython Itertools
|
Python ItertoolsItertool is one of the most amazing Python 3 standard libraries. This library has pretty much coolest functions and nothing wrong to say that it is the gem of the Python programing language. Python provides excellent documentation of the itertools but in this tutorial, we will discuss few important and useful functions or iterators of itertools. The key thing about itertools is that the functions of this library are used to make memory-efficient and precise code. Before learning the Python itertools, you should have knowledge of the Python iterator and generators. In this article, we will describe itertools for beginners are well as for professionals. IntroductionAccording to the official definition of itertools, "this module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML." In simple words, the number of iterators can together create 'iterator algebra' which makes it possible to complete the complex task. The functions in itertools are used to produce more complex iterators. Let's take an example: Python built-in zip() function accepts any number of arguments as iterable. It iterates over tuples and return their corresponding elements.
Output: [(1, 'a'), (2, 'b'), (3, 'c')] In the above code, we have passed two lists [1,2,3] and ['a', 'b', 'c'] as iterable in zip() function. These lists return one element at a time. In Python, an element that implement .__iter__() or .__getitem__() method called iterable. The Python iter() function is used to call on the iterable and return iterator object of the iterable.
Output: <str_iterator object at 0x01505FA0> The Python zip() function calls iter() on each of its argument and then calls next() by combining the result into tuple. Note: If you are using the zip() function and map() function that means you are already using itertools. You don't need to import it distinctly.Types of IteratorThere are various types of iterator in itertools module. The list is given below:
Infinite IteratorsIn Python, any object that can implement for loop is called iterators. Lists, tuples, set, dictionaries, strings are the example of iterators but iterator can also be infinite and this type of iterator is called infinite iterator.
Output: 10 15 20 25 30 35 40 45
Output: 1 2 3 1 2 3 1 2 3 1 2 Example - 2: Using next() function
Output: Java T Point Java T Point
Output: [40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40] Combinatoric iterators: The complex combinatorial constructs are simplified by the recursive generators. The permutations, combinations, and Cartesian products are the example of the combinatoric construct. In Python, there are four types of combinatoric iterators:
Output: Computing cartesian product using repeat Keyword Argument:
[(1, 1), (1, 2), (2, 1), (2, 2)]
Computing cartesian product of the containers:
[('Java', '5'), ('T', '5'), ('point', '5')]
Computing product of the containers:
[('C', 4), ('C', 5), ('D', 4), ('D', 5)]
Output: Computing all permutation of the following list
[(3, 'Python'), ('Python', 3)]
Permutations of following string
[('A', 'B'), ('B', 'A')]
Permutation of the given container is:
[(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 3), (3, 0), (3, 1), (3, 2)]
Output: Combination of list in sorted order(without replacement) [('B', 3)]
Combination of string in sorted order [('Z', 'X')]
Combination of list in sorted order [(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,)]
Output: Combination of string in sorted order(with replacement) is:
[('X', 'X', 'X'), ('X', 'X', 'Y'), ('X', 'Y', 'Y'), ('Y', 'Y', 'Y')]
Combination of list in sorted order(with replacement) is:
[(4, 4, 4), (4, 4, 2), (4, 2, 2), (2, 2, 2)]
Combination of container in sorted order(with replacement) is:
[(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)]
Terminating IteratorTerminating iterators are generally used to work on the small input sequence and generate the output based on the functionality of the method used in iterator. There are different types of terminating iterator:
Output: The sum is : [1, 5, 10, 17, 26, 37] The product is : [1, 4, 20, 140, 1260, 13860] The sum is : [1, 5, 10, 17, 26, 37] The product is : [1, 4, 20, 140, 1260, 13860]
Output: The output is: [1, 2, 3, 4, 1, 5, 6, 8, 9, 10, 11, 12]
Output: The output is : [5, 7, 8]
Output: The Output is : [15, 27]
Output: The sliced list values are : [34, 73, 19]
Output: The values acc. to function are : [20, 40, 90, 27]
Output: The list values until false value return : [20, 42, 64]
Output: (<itertools._tee object at 0x01B88D88>, <itertools._tee object at 0x01B88DA8>, <itertools._tee object at 0x01B88BA8>) The iterators are : [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7]
Output: The combined value of iterables is :
('J', 'T') ('a', 'p') ('v', 'o') ('a', 'i') ('_', 'n') ('_', 't')
In this tutorial, we have discussed several useful iterators along with itertools.
Next TopicPython Multiprocessing
|
Python MultiprocessingIn this article, we will learn how we can achieve multiprocessing using Python. We also discuss its advanced concepts. What is Multiprocessing?Multiprocessing is the ability of the system to run one or more processes in parallel. In simple words, multiprocessing uses the two or more CPU within the single computer system. This method is also capable to allocate the tasks between more than one process. Processing units share the main memory and peripherals to process programs simultaneously. Multiprocessing Application breaks into smaller parts and runs independently. Each process is allocated to the processor by the operating system. Python provides the built-in package called multiprocessing which supports swapping processes. Before working with the multiprocessing, we must aware with the process object. Why Multiprocessing?Multiprocessing is essential to perform the multiple tasks within the Computer system. Suppose a computer without multiprocessing or single processor. We assign various processes to that system at the same time. It will then have to interrupt the previous task and move to another to keep all processes going. It is as simple as a chef is working alone in the kitchen. He has to do several tasks to cook food such as cutting, cleaning, cooking, kneading dough, baking, etc. Therefore, multiprocessing is essential to perform several task at the same time without interruption. It also makes easy to track all the tasks. That is why the concept of multiprocessing is to arise.
In the multiprocessing, the CPU can assign multiple tasks at one each task has its own processor. Multiprocessing In PythonPython provides the multiprocessing module to perform multiple tasks within the single system. It offers a user-friendly and intuitive API to work with the multiprocessing. Let's understand the simple example of multiple processing. Example -
Output: 'Hello !! Welcome to Python Tutorial' Explanation: In the above code, we have imported the Process class then create the Process object within the disp() function. Then we started the process using the start() method and completed the process with the join() method. We can also pass the arguments in the declared function using the args keywords. Let's understand the following example of the multiprocessing with arguments. Example - 2
Output: The Cube is: 125 The Square is: 25 Both processes are finished Explanation - In the above example, We created the two functions - the cube() function calculates the given number's cube, and the square() function calculates the square of the given number. Next, we defined the process object of the Process class that has two arguments. The first argument is a target that represents the function to be executed, and the second argument is args that represents the argument to be passed within the function.
We have used the start() method to start the process.
As we can see in the output, it waits to completion of process one and then process 2. The last statement is executed after both processes are finished. Python Multiprocessing ClassesPython multiprocessing module provides many classes which are commonly used for building parallel program. We will discuss its main classes - Process, Queue and Lock. We have already discussed the Process class in the previous example. Now we will discuss the Queue and Lock classes. Let's see the simple example of a get number of CPUs currently in the system. Example -
Output: ('The number of CPU currently woking in system : ', 32)
The above number of CPUs can vary for your pc. For us, the number of cores is 32. Python Multiprocessing Using Queue ClassWe know that Queue is important part of the data structure. Python multiprocessing is precisely the same as the data structure queue, which based on the "First-In-First-Out" concept. Queue generally stores the Python object and plays an essential role in sharing data between processes. Queues are passed as a parameter in the Process' target function to allow the process to consume data. The Queue provides the put() function to insert the data and get() function to get data from the queues. Let's understand the following example. Example -
Output: pushing items to the queue:
('item no: ', 1, ' ', 'Apple')
('item no: ', 2, ' ', 'Orange')
('item no: ', 3, ' ', 'Guava')
('item no: ', 4, ' ', 'Papaya')
('item no: ', 5, ' ', 'Banana')
popping items from the queue:
('item no: ', 0, ' ', 'Apple')
('item no: ', 1, ' ', 'Orange')
('item no: ', 2, ' ', 'Guava')
('item no: ', 3, ' ', 'Papaya')
('item no: ', 4, ' ', 'Banana')
Explanation - In the above code, we have imported the Queue class and initialized the list named fruits. Next, we assigned a count to 1. The count variable will count the total number of elements. Then, we created the queue object by calling the Queue() method. This object will used to perform operations in the Queue. In for loop, we inserted the elements one by one in the queue using the put() function and increased the count by 1 with each iteration of loop. Python Multiprocessing Lock ClassThe multiprocessing Lock class is used to acquire a lock on the process so that we can hold the other process to execute a similar code until the lock has been released. The Lock class performs mainly two tasks. The first is to acquire a lock using the acquire() function and the second is to release the lock using the release() function. Python Multiprocessing ExampleSuppose we have multiple tasks. So, we create two queues: the first queue will maintain the tasks, and the other will store the complete task log. The next step is to instantiate the processes to complete the task. As discussed previously, the Queue class is already synchronized, so we don't need to acquire a lock using the Lock class. In the following example, we will merge all the multiprocessing classes together. Let's see the below example. Example -
Output: Task no 2 Task no 5 Task no 0 Task no 3 Task no 6 Task no 1 Task no 4 Task no 7 Task no 0 is done by Process-1 Task no 1 is done by Process-3 Task no 2 is done by Process-2 Task no 3 is done by Process-1 Task no 4 is done by Process-3 Task no 5 is done by Process-2 Task no 6 is done by Process-1 Task no 7 is done by Process-3 Python Multiprocessing PoolPython multiprocessing pool is essential for parallel execution of a function across multiple input values. It is also used to distribute the input data across processes (data parallelism). Consider the following example of a multiprocessing Pool. Example -
Output: Process name is V waiting time is 5 seconds Process V Executed. Process name is X waiting time is 2 seconds Process X Executed. Process name is Y waiting time is 1 seconds Process Y Executed. Process name is Z waiting time is 3 seconds Process Z Executed. Let's understand another example of the multiprocessing Pool. Example - 2
Output: [1, 8, 27] Proxy ObjectsThe proxy objects are referred to as shared objects which reside in a different process. This object is also called as a proxy. Multiple proxy objects might have a similar referent. A proxy object consists of various methods which are used to invoked corresponding methods of its referent. Below is the example of proxy objects. Example -
Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] <ListProxy object, typeid 'list' at 0x7f063621ea10> 16 [4, 9, 16] The proxy objects are picklable so we can pass them between processes. These objects are also used for level of control over the synchronization. Commonly Used Functions of MultiprocessingSo far, we have discussed the basic concepts of multiprocessing using Python. Multiprocessing is a broad topic itself and essential for performing various tasks within a single system. We are defining a few essential functions that are commonly used to achieve multiprocessing.
Next TopicPython OOPs Concepts
|
How to Calculate Distance between Two Points using GEOPY.The geopy is a Python library which helps to calculate geographical distance. In this tutorial, we will discuss different methods of how the user can calculate the distance between two places on the earth. First, the user has to install the geopy by using the following command:
After successful installation, we are ready to work with the geopy library. Calculate Distance between Two PointsBelow are the important methods that used to calculate the distance between two points. Method 1: By using Geodesic DistanceThe geodesic distance is the length of the shortest path between two points on any surface of Earth. In the following example, we will show how the user can calculate the Geodesic Distance from the latitude and longitude data. Example:
Output: The distance between New York and Texas is: 2507.14797665193 Method 2: By using Great Circle DistanceThe great circle distance is the shortest path between two points on the sphere. In this case, we will assume the earth is the perfect sphere. The following example shows how the user can calculate great circle distance by using longitude and latitude data of two points. Example:
Output: The distance between New York and Texas is: 2503.045970189156 Method 3: By using Haversine FormulaThe orthodromic distance is used for calculating the shortest distance between two latitudes and longitudes points on the earth's surface. Using this method, the user needs to have the coordinates of two points (P and Q). First, they have to convert the values of latitude and longitude points from decimal degrees to radians and then divide the values of latitude and longitude by (180/π). The user should use the value of "π = 22/7". Then, the value of (180/π) will be "57.29577". If the user wants to calculate the distance in miles, they can use the value of the radius of Earth, that is, "3,963". And if the user wants to calculate the distance in Kilo-metre, they can use the value "6,378.80". Formulas:
The user needs the coordinates of P point and Q points in terms of longitude and latitude, then using the above formula for converting them into radians. Now, calculate the distance between two points by using the following formula. Formula: For miles:
For kilometre:
Thus, the user can calculate the shortest distance between the two given points on Earth by using Haversine Formula. Example:
Output: The distance between New York and Texas is: 2503.04243426357 K.M The distance between New York and Texas is: 1556.985899699659 Miles ConclusionIn this tutorial, we have discussed various methods for calculating the distance between two points on the earth's surface by using the geopy library. We have shown examples of each method.
Next TopicGmail API in Python
|
Gmail API in PythonIn this tutorial, we are going to learn about Gmail API in Python, and we will also learn how we can use Gmail APIs in Python to perform many Gmail operations such as sending an email, searching an email, deleting an email, etc. For this, we will learn to set up Gmail API in our Python script. First, let us have a brief of Gmail API and its basic introduction. Gmail APIsGmail is the most popular mail service in today's world, and it is used by almost all of us and many organizations. Over past years, many Gmail features are enhanced with the use of AI, including suggestions while composing emails and security features (detecting fraud and spam emails). Gmail API is APIs based on RESTful APIs that allow its users to interact with our Gmail account, and it helps us to use its features using a Python script. Prerequisites of Using Gmail APIs in PythonWe must fulfil the following requirements for using Gmail APIs in our Python script:
Installation of Required libraries:Before enabling the Gmail APIs to use them in our Python script, let's first install the pre-required libraries in our system. To install the pre-required libraries for enabling the Gmail APIs, we should follow the following steps: Step 1: Open the command prompt terminal of the system and make sure that our device has an active internet connection. Step 2: Write down the following command in the terminal:
Now, press enter to start the installation of libraries.
As we can see that, the pre-required libraries for enabling Gmail APIs are successfully installed in our system. Now, we can proceed with enabling Gmail APIs part in this tutorial. Enabling Gmail APIs in our deviceWe have to follow the following given steps to enable Gmail APIs in our device so that we can use these APIs in our Python script: Step 1: Creating New Project on Google Cloud console: In this step, first, we have to login into the Google cloud console (https://console.cloud.google.com/?pli=1) with our Google account, and then we have to click on 'New Project' to create a new project.
If we already have an existing project, then we can also continue with the existing project. Step 2: Now, we have to go to the API and services option from the Project menu that we have created.
Step 3: Now, we can see the option 'Enable Gmail API and services,' and we have to choose this option to enable Gmail APIs for the project.
Step 4: Configuration of Consent screen: Now, in this step, we will configure the consent screen of the project we created by clicking on the 'OAuth Consent Screen' option given in the menu. We can only see this option if the consent screen is not already configured.
Step 5: Now, we have to enter the application name of our choice and save the project. Step 6: Now, click on the credentials option and go to credentials.
Step 7: Creating an OAuth Client ID: Now, we click on the 'create credentials' option and go to the OAuth Client ID to create it. We perform this by following the below sequential procedure to create a new OAuth Client ID for our project:
Now, we are done with all the steps of enabling Gmail APIs, and we can start using Gmail APIs in our Python script. Note: We have to save the client ID and password so that we can use them in future references if required.Importing Necessary Modules Now, we have set up all the necessary APIs and we should forward with importing all the necessary modules. Let's see the below example of importing modules. Example -
Output: Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=107196167488-dh4b2pmpivffe011kic4em9a4ugrcooi.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A55991%2F&scope=https%3A%2F%2Fmail.google.com%2F&state=kfXlNyjvbKetyUK0op7OF9WY7shrKS&access_type=offline
Explanation - When we run the above given program, we will see an option to choose the browser, as we can see in the above image, and if we don't see an option like this, we need to click on the link given in the output. Then, we can select the browser of our choice or the default browser of the system to continue the process. Now, when we select the browser, we will be redirected to our browser and can see the following tab opened in it:
Now, we tick the checkbox option shown in the dialogue box to give the required permissions, and then, we will have to click on the continue option. After clicking on continue, we can see the following window will open in the same tab:
As the window is showing, the authentication part for enabling Gmail API is completed, and we have linked our Gmail account with the project for Gmail APIs we created. Note: Of course, we have to put our mail that we can connect to Gmail APIs and use for future references for working with Gmail APIs, in the place of 'OurMail@gmail.com' as provided in the above program.Performing Actions using Gmail APIs in PythonNow, we have completely set up and enabled Gmail APIs in our project with Python script. Now, we can perform many actions from our Gmail account with a Python program. We can perform the following Gmail actions with our Python script using Gmail APIs in it:
In this tutorial, we will only about sending an email using Gmail APIs in our Python program, and we will learn to write the code to perform this action with Python script. Sending an emailWe can simply write and send an email by writing a Python program and using enabled Gmail APIs in it. Here, in this section, we will write a Python program by which we can send emails from our Gmail account just by running the program. Look at the following Python program for a better understanding of it: Example -
Output:
If we put our mail in the place of the receiver's mail, i.e., Reciever@gmail.com, we will find that the mail is actually sent to the mail we entered as receiver's mail when we run the program, same as what we can see in the above output image. ConclusionTo use the Gmail APIs with our Python script or simply in Python, first, we have to enable them, and create a Project in Google cloud with our Gmail account. We can also perform many other actions like reading, deleting, etc., using Gmail APIs in our Python program like sending emails. We can also modify many things into our Gmail account that we authenticated with our Gmail APIs project, just by running our Python scripts (enabled with Gmail APIs). |
How to Plot the Google Map using folium package in PythonThe folium package is built on the data wrangling strengths of the Python ecosystem and the mapping strengths of the Leaflet.js library of JavaScript language. The user can manipulate their data by using Python and then visualize it by using Leaflet.js map through folium package. Folium package is an easy approach of visualizing the data on Leaflet.js map, which has been manipulated by using Python. Required Module and LibrariesFolium: The user can install the Folium package by using the following command.
Geopy: The geopy module of Python makes it easy for Python users to locate the coordinates of landmarks, cities, countries on the earth's surface. For installing the geopy module, the user can use the following command:
After successful installation of the both libraries, we follow the below steps to plot Google map. Step 1: Create the Base mapThe user can create the base map by using the following program:
Output:
Step 2: Add a Circular MarkerThe user can mark the area with the circle and popup text by using the following code:
Output:
Step 3: Add the simple marker for the parachute style marker with the popup textThe user can use the following code. Example -
Output:
Step 4: Add the line on the mapThe user can use the following code for adding the line on the map to join the two coordinates. Example -
Output:
Explanation We used the geopy library to get the latitude and longitude of the location. Then we used the "folium.map" method of the folium package for creating the base of Google Maps. In step 2, we used "folium.CircleMarker" for marking the circular mark on the location with the pop-up text. In step 3, we used "folium.Marker" to add a parachute style mark on the mentioned location. In the last step, we used "folium.PolyLine" for joining two marks on two different locations on the map. ConclusionIn this tutorial, we have shown how the user can Plot the Google map and add different required functionalities on the map like a circular mark, parachute mark, pop-up text, and the line joining the two coordinates on the map.
Next TopicGrid Search in Python
|
Grid Search in PythonIn this tutorial, we will discuss the Grid Search for the purpose of hyperparameter tuning. We will also learn about the working of Grid Search along with the implementation of it in optimizing the performance of the method of Machine Learning (ML). Hyperparameter tuning is significant for the appropriate working of the models of Machine Learning (ML). A method like Grid Search appears to be a basic utility for hyperparameter optimization. The Grid Search Method considers some hyperparameter combinations and selects the one returning a lower error score. This method is specifically useful when there are only some hyperparameters in order to optimize. However, it is outperformed by other weighted-random search methods when the Machine Learning model grows in complexity. So let us begin by understanding Grid Search. Understanding Grid SearchGrid Search is an optimization algorithm that allows us to select the best parameters to optimize the issue from a list of parameter choices we are providing, thus automating the 'trial-and-error' method. Although we can apply it to multiple optimization issues; however, it is most commonly known for its utilization in machine learning in order to obtain the parameters at which the model provides the best accuracy. Let us consider that the model accepts the below three parameters in the form of input:
If we want to try out two options for every parameter input (as specified in square brackets above), it estimates different combinations. For instance, one possible combination can be [2, 5, 10]. Finding such combinations manually would be a headache. Now, suppose that we had ten different parameters as input, and we would like to try out five possible values for each and every parameter. It would need manual input from the programmer's end every time we like to alter the value of a parameter, re-execute the code, and keep a record of the outputs for every combination of the parameters. Grid Search automates that process, as it accepts the possible value for every parameter and executes the code in order to try out each and every possible combination outputs the result for the combinations and outputs the combination having the best accuracy. Installing the required librariesBefore we start implementing the Grid Search in the Python programming language, let us briefly discuss some of the necessary libraries and frameworks that need to be installed in the system. These libraries and frameworks are as follows: They are all quite simple to install. We can use the pip installer in order to install these libraries as shown below:
Note: If any issues arise while executing any package, try reinstalling and referring to each package's official documentation.Now, let us begin implementing the Grid Search in Python Implementation of Grid Search in PythonIn the following section, we will understand how to implement Grid Search on an actual application. We will simply be executing the code and discuss in-depth regarding the section where Grid Search comes in rather than discussing Machine Learning and Data Pre-processing section. So, let's get started. We will use the Diet Dataset containing data regarding the height and weight of different people based on various attributes such as gender, age, and type of diet. We can directly import the data from an online resource with the help of the Pandas read_csv() function. But before that, we have to import the required packages: File: mygrid.py
Explanation: In the above snippet of code, we have imported the required packages and libraries necessary for the project. One can also save the program file and execute it in order to check if the libraries and packages are installed and imported properly. Once the packages are imported successfully, we have to use the following snippet of code in order to import the dataset and print the first five rows of it. File: mygrid.py
Output: Person gender Age Height pre.weight Diet weight6weeks 0 25 41 171 60 2 60.0 1 26 32 174 103 2 103.0 2 1 0 22 159 58 1 54.2 3 2 0 46 192 60 1 54.0 4 3 0 55 170 64 1 63.3 Explanation: In the above snippet of code, we have imported the dataset using the read_csv() of the pandas library and stored it within the mydf variable. We have then printed the first five rows using the head() function along with the mydf variable. Now, let us divide the data into the feature and label sets in order to apply the standard scaling on the dataset. The snippet of code for the same is shown below: File: mygrid.py
Explanation: In the above snippet of code, we have converted the pandas dataframe into a NumPy array. We have then imported the StandardScaler module from the sklearn library and use the function to normalize the data. We have then transformed and displayed the training data using the transform() function. Now, let us consider the following snippet of code in order to create a simple deep learning model. File: mygrid.py
Explanation: The following snippet of code has defined a function as create_my_model() accepting two parameters, i.e., learnRate and dropoutRate, respectively. We have then created the model as mymodel using the Sequential() function. We have also used the add() along with the Dense() and Dropout() function. We have then compiled the model using the compile() function. As a result, when we execute the code, this will lead to loading the dataset, preprocessing it, and creating a machine learning model. Since we are only interested in understanding the working of Grid Search, we haven't performed the train/test split, and we had fitted the model on the complete dataset. Now, we will understand how Grid Search makes the programmer's life easier by optimizing the parameters in the next section. Training the Model without Grid SearchIn the snippet of code shown below, we will create a model with the help of parameter values that we decided on randomly or based on our intuition and see how our model performs: File: mygrid.py
Output: 4/4 [==============================] - 41s 14ms/step - loss: 0.9364 - accuracy: 0.0000e+00 Explanation: In the above snippet of code, we have declared the values of the parameter, i.e., dropoutRate, epochs, batchSize, and learnRate, respectively. We have then created the model object by calling the create_my_model() function. We have then fitted the model onto the training data. As a result, the accuracy we got is 0.0000e+00. Optimizing Hyper-parameters using Grid SearchIf we are not using the Grid Search method, we can directly call the fit() function on the model we have created above. But in order to utilize the Grid Search method, we have to pass in few arguments to the create_my_model() function. Moreover, we have to declare the grid with various options to try for every parameter. Let us perform that in different parts. First of all, we will try modifying the create_my_model() function in order to accept arguments from the calling function as shown below: File: mygrid.py
Explanation: In the above snippet of code, we have made some changes to the previous create_my_model function and used the KerasClassifier to create the model object. Now, let us implement the algorithm for Grid Search and fit the dataset on it: File: mygrid.py
Output: Best: 0.00347268912077, using {batch_size=10, dropoutRate=0.4, epochs=5, learnRate=0.2}
Explanation: The above output shows the parameter combination which yields the best accuracy. At last, we can conclude that the Grid Search is quite easy to implement in the Python programming language and saved us a lot of time in human labor. We can list down all the arguments we wanted to tune, declare the values that need to be tested, execute the code, and forget about it. The process is so easy and convenient that it requires less input from the programmer's side. Once the best argument combination has been found, we can utilize that for the final model.
Next TopicPython High Order Function
|
Python High Order FunctionAs we must be aware of the basic concept of Python functions, we should move forward with some advanced concepts related to Python functions. In this tutorial, we are going to discuss the important aspects of High order functions in Python, like what high order functions are, how we can define them in Python, how we can use them in Python and what the properties of high order functions are. Prerequisites:To learn about high order functions in Python, we must have basic knowledge of the following concepts:
First, let's start with the first thing, i.e., High order functions, and understand a basic about them. High order functionsA function that is having another function as an argument or a function that returns another function as a return in the output is called the High order function. High order functions operate with other functions given in the program. A fact about the High order function is that a high order function is applicable to both functions as well as to the methods that take a function as their parameter or return a function as the result of them. In Python, this concept of high-order functions is supported with every aspect. Properties of High order functions in PythonNow, in this section, we will discuss some of the important properties of high order functions that are applicable in Python as well.
High order functions in PythonNow, in this section, we will talk specifically about the Python high order functions and how we can define them. We will discuss the methods and means by which we will define and use high order functions in our Python program. Following are the ways to define High order functions in a Python code that we are going to discuss in this tutorial.
Now, we will discuss each of the above-given methods in detail and learn about their implementation as high order functions in a Python program. Method 1: Using functions as objects in High order functionIn Python, we can even assign a given function to a variable also. This assignment of function into variable will not call the actual function, instead of it will create a reference to the function that is created. Thus, it makes this assignment of assigning a function as a variable object will create a high order function in the program. Look at the following example program to learn the implementation of method we discussed above: Example -
Output: Enter a text to print it in uppercase and double: JavaTPoint JAVATPOINT JAVATPOINT Method 2: Functions as a parameter for another functionBasically, Python functions are like Python objects, and therefore we can use Python functions to pass them as an argument inside another function, and that will create a high order function in the program. Look at the following program to understand the implementation of above-given method: Example -
Output: HELLO PYTHON DEVELOPERS! YOU ARE WELCOMED TO JAVATPOINT hello python developers! you are welcomed to javatpoint Method 3: Returning function as a result in high order functionWe can also return a function as the result of another function as an object, and that makes the function a high order function. Look at the following example program to learn the implementation of method we discussed above: Example -
Output: Enter First Number: 24 Enter Second Number: 26 Sum of Two numbers given by you is: 50 Method 4: Decorators as high order functionWe can use decorators as the high order function as the most commonly used high order function in Python. Decorators in Python allow us to modify the behavior of methods or functions we defined in the program, and it also allows us to wrap a function inside another function to extend the behavior of wrapped or parent function. We can even wrap a function inside another function without even permanently modifying the parent function. In Python decorators, a function is taken as an argument for the other function, and then these decorators are called inside the wrapped function. Look at the following exemplar syntax for a decorator defined in a Python program. Syntax
The above syntax for the decorator is equivalent to the following Python code for a high order function.
We have referred @JTP_Decorator as a callable function inside the default Python_Decorator() function in the above-given code. We will have to add just some extra code in this structure, and we will get the output as the wrapper function. Look at the following program to understand the implementation of above given method: Example -
Output: This line of code will be printed before the execution of high order function This line of code will be printed inside the execution of high order function This line of code will be printed after the execution of high order function
Next TopicAssignment Operators in Python
|
nsetools in PythonIn the following tutorial, we will discuss the nsetools library in the Python programming language. We will understand its features and work with some examples. So, let's get started. Understanding the nsetools libraryNSE or National Stock Exchange of India Limited is the leading stock exchange of India, situated in Mumbai, Maharashtra. NSE was established in the year 1992 as the first dematerialized electronic exchange in the country. Python offers a library that allows the programmers to collect real-time data from National Stock Exchange (India). This library is known as nsetools. We can use this library in different projects, which requires fetching live quotes for a provided index or stock or creating large sets of data for further data analytics. We can also create Command-Line Interface (CLI) Applications that may deliver us the details of the live market at a blazing fast speed, pretty faster than any web browser. The data accuracy is only as correct as provided on the official website of the National Stock Exchange of India Limited. (http://www.nseindia.com) Main features of the Python nsetools librarySome of the key features of the Python nsetools library are stated as follows:
How to install the Python nsetools library?The installation part of the nsetools library is quite easy, and it has no external dependencies. All the dependencies of the library are part of standard distribution packages of Python. We can install the nsetools library using the pip installer as shown in the following syntax: Syntax:
Updating the libraryIf some of us already have installed the nsetools library in their systems, then the following command will allow them to update the library. Syntax:
Python 3 supportPython 3 support for the library has been included from version 1.0.0 and so on. Now, this library is able to work for both Python 2 as well as Python 3. Creating an NSE objectWe can create an NSE object using the Nse() function offered by the nsetools library. The same can be seen in the following example: Example:
Output: NSE Object: Driver Class for National Stock Exchange (NSE) Explanation: In the above snippet of code, we have imported the required function from the library. We have then defined a variable that uses the Nse() function to create an NSE object. We have then printed the value of the variable for the users. Getting Information using the nsetools libraryLet us consider an example demonstrating the use of nsetools for gathering Information. Example:
Output: State Bank of India Average Price: 431.97 Explanation: In the above snippet of code, we have imported the required module and created an NSE object using the Nse() function. We have then defined another variable that uses the get_quote() function on the NSE object to get the quotation of the specified company. We have then printed the required details for the users. |
Python program to find the nth Fibonacci NumberIn the following tutorial, we will understand how to find the nth Fibonacci Number using Python. We can define a Fibonacci Number, where the following number is the sum of the preceding two numbers. The first two elements of the Fibonacci series are 0 and 1, respectively. We can calculate the third element of the series by adding the preceding two elements and will get the third term as 0 + 1, which is equal to 1. Similarly, the fourth term will be the sum of the second and third terms, which is 1 + 1 = 2 and so on. The series of such numbers is known as a Fibonacci Series. The recurrence relation defines a Fibonacci number as shown below: Fn = Fn - 1 + Fn - 2 There are different ways to find the nth Fibonacci Number using the Python programming language. Some of them are as follows:
Of these ways, the two most fundamental are the Recursion method and the Dynamic method. Let us understand the working of these methods in detail with examples. Finding nth Fibonacci Number using RecursionThe term recursion is used to define something within itself. In a programming language like Python, Recursion refers to the process of a function calling itself. With the proper and correct code, the Recursion will create a finite loop. Let us consider the following snippet of code for better understanding. Example:
Output: 12th Element of the Fibonacci Series: 144 Explanation: In the above snippet of code, we have defined a function as Fibonacci_Series() that accepts a parameter as n. Moreover, we are aware that the first two elements of the Fibonacci are 0 and 1. In the event of the input as n = 1 or n = 2 (First or Second terms of Fibonacci series), we have used the if-else conditional statement to return 0 or 1. In case the value of n is greater than 2, the function will call itself with a lower input value. As we can observe that the code returns (Fibonacci_Series(n - 1) + Fibonacci_Series(n - 2)). Here, the function calls itself with a lower value unless it reaches the base value of n = 1 and n = 2, and as we know from before, n = 1 returns 0 and n = 2 returns 1. The returned values are then continuously added to produce the sequence of the Fibonacci Series. Finding the nth Fibonacci Number using Dynamic ProgrammingDynamic Programming utilizes Recursion as well; however, it mainly utilizes if-else conditional statements. Within the statements, the value of the Fibonacci number is stored in a variable. With the help of Recursion, the repeated addition allows us to obtain this Fibonacci number. Let us consider the following example to understand the same. Example:
Output: 12th Term of Fibonacci Series: 144 Explanation: In the above snippet of code, we defined the function as Fibonacci_series(), which accepts the parameter as variable x. We created a one-dimensional array as fib_Array with data elements 0 and 1 in its zeroth and first indices. Then, if the provided input ('x') is less than or equal to 2, which is also the length of the array fib_Array, it returns 0 as the first number for x = 1 and 1 as the second number for x = 2. If the value of x is greater than 2, we have used recursion to call and insert the preceding two data elements. However, rather than returning the nth Fibonacci number directly, we append each of the summated elements to the fib_Array array. At last, we have returned the last element of the array (i.e., the nth element) and printed the value for the users. Finding the nth Fibonacci Number using Dynamic Programming and Space OptimizationThis method is almost completely identical to Dynamic Programming. However, dynamic programming utilizes recursion to accomplish recurring addition, whereas this method utilizes the for-loop. Let us consider the following example to understand the same. Example:
Output: 12th element of the Fibonacci Series: 144 Explanation: In the above snippet of code, we have defined a function and assigned two variables, m = 0 and n = 1. These elements are the first and second elements of the Fibonacci Series. We have then used the if-elif-else conditional statements where the program returns 0 for input value x = 1 and 1 for input value x = 2. If the value of x is greater than 2, we have used the for-loop of i in the range (2, x + 1). We have taken a variable o to store the sum of the preceding two elements in the series. Once o takes the value of m + n, the value of m is reassigned to n. Subsequently, the value of n is reassigned to the value of o. This process continues, and value 3 keeps reassigning until the loop terminates. Once the loop is terminated, the function returns the value of n, which stores the value of the nth Fibonacci Number. Finding the nth Fibonacci Number using ArrayIn this method, we create an array of size x by repeated addition using the for-loop. Hence, the nth Fibonacci Number is returned. Let us consider the following example to understand the same. Example:
Output: 12th element of the Fibonacci series: 144 Explanation: In the above snippet of code, we have defined the function. Within the function, we have created an array to find the nth element of the Fibonacci Series. We have then used the for-loop to add elements of the series to the array by repeating the addition of the preceding two elements. At last, the nth element is returned and printed for the users.
Next TopicPython OpenCV object detection
|
Python OpenCV object detectionOpenCV is the huge and open-source library for image processing, machine learning and computer vision. It is also playing an important role in real-time operation. With the help of the OpenCV library, we can easily process the images as well as videos to identify the objects, faces or even handwriting of a human present in the file. We will only focus to object detection from images using OpenCV in this tutorial. We will learn about how we can use OpenCV to do object detection from a given image using a Python program. Object DetectionBasically, object detection is a modern computer technology that is related to image processing, deep learning and computer vision to detect the objects present in an image file. All the technologies used in the Object detection technique (as we mentioned earlier) deals with detecting instances of the object in the image or video. Object Detection using OpenCVWe have learned about object detection in the previous section, and in this section, we will learn that how we can do object detection in an image or video using the OpenCV library. We will first import the OpenCV library in the Python program, and then we will use functions to perform object detection on an image file given to us. But, before using and importing the library functions, let's first install the requirements for using the Object detection technique. In this tutorial, we will use the Haar cascade technique to do object detection. Let's learn in brief about the Haar cascade technique first. Haar cascade:Basically, the Haar cascade technique is an approach based on machine learning where we use a lot of positive and negative images to train the classifier to classify between the images. Haar cascade classifiers are considered as the effective way to do object detection with the OpenCV library. Now, let's understand the concept of positive and negative images that we have discussed earlier:
Requirements for object detection with Python OpenCV:We have to install first some important libraries in our system as it is an important requirement for doing object detection tasks. We have to install the following libraries into our system as the requirement for performing object detection: 1. Installation of OpenCV library:First and foremost, the requirement to perform object detection using the OpenCV library is that the OpenCV library should be present in our device so that we can import it into a Python program and use its object detection functions. If this library is not present in our system, we can use the following command in our command prompt terminal to install it:
When we press the enter key after writing this command in the terminal, the pip installer in the command prompt will start installing the OpenCV library into our system.
As we can see that, the OpenCV library is successfully installed in our system, and now we can import it into a Python program to use its functions. 2. Installation of matplotlib library:Matplotlib is very helpful in the opening, closing, reading etc., images in a Python program, and that's why the installation of this library for object detection becomes an important requirement. If the matplotlib library is not present in our system, we have to use the following command in our command prompt terminal to install it:
When we press the enter key after writing this command in the terminal, the pip installer in the command prompt will start installing it into our system.
As we can see that, the matplotlib library is successfully installed in our system, and now we can import it into a Python program to use its functions for opening, reading etc., images. We have installed all the required libraries for performing object detection, and now we can move ahead with the implementation part of this task. Implementation of Object detection in Python:In this part, we will write the Python programs to do the object detection and understand the implementation of it. We will use the following image in our Python program to perform the object detection on it:
Opening the ImageWe will first open the image given above and create the environment of the picture to show it in the output. Let's first look at an example program to understand the implementation, and then we will look at the explanation part. Example 1: Opening the image using OpenCV and matplotlib library in a Python program:
Output:
Explanation: First, we have imported the OpenCV (as cv2) and matplotlib (as plt) libraries into the program to use their functions in the code. After that, we have opened the image file using the imread() function of cv2. Then, we have defined the properties for the image we opened in the program using the cv2 functions. Then, we subplot the image using the subplot() function of plt and giving parameters in it. In last, we have used the imshow() and show() function of the plt module to show the image in the output. As we can see in the output, the image is displayed as a result of the program, and its borders have been sub-plotted. Recognition or object detection in the imageNow, we will use the detectMultiScale() in the program to detect the object present in the image. Following is the syntax for using detectMultiScale() function in the code:
We will use a condition statement with this function in the program to check if any object from the image is detected or not and highlight the detected part. Let's understand the implementation of object detection in the image through an example program. Example 2: Object detection in the image using the detectMultiScale() in the following Python program:
Output:
Explanation: After opening the image in the program, we have imported the cascade classifier XML file into the program. Then, we used the detectMultiScale() function with the imported cascade file to detect the object present in the image or not. We used if condition in the program to check that object is detected or not, and if the object is detected, we have highlighted the detected object part using for loop with cv2 functions. After highlighting the detected object part in the image, we have displayed the processed image using the plt show() and imshow() function. As we can see in the output, the image with the object detected part as highlighted is shown to us when we run the program.
Next TopicPython SimpleImputer module
|
Python SimpleImputer moduleIn this tutorial, we are going to learn about the SimpleImputer module of the Sklearn library, and it was previously known as impute module but updated in the latest versions of the Sklearn library. We will discuss the SimpleImputer class and how we can use it to handle missing data in a dataset and replace the missing values inside the dataset using a Python program. SimpleImputer classA scikit-learn class that we can use to handle the missing values in the data from the dataset of a predictive model is called SimpleImputer class. With the help of this class, we can replace NaN (missing values) values in the dataset with a specified placeholder. We can implement and use this module class by using the SimpleImputer() method in the program. Syntax for SimpleImputer() method:To implement the SimpleImputer() class method into a Python program, we have to use the following syntax:
Parameters: Following are the parameters which has to be defined while using the SimpleImputer() method:
SimpleImputer class is the module class of Sklearn library, and to use this class, first we have to install the Sklearn library in our system if it is not present already. Installation of Sklearn library:We can install the Sklearn by using the following command inside the command terminal prompt of our system:
After pressing the enter key, the sklearn module will start installing in our device, as we can see below:
Now, the Sklearn module is installed in our system, and we can move ahead with the SimpleImputer class function. Handling NaN values in the dataset with SimpleImputer classNow, we will use the SimpleImputer class in a Python program to handle the missing values present in the dataset (that we will use in the program). We will define a dataset in the example program while giving some missing values in it, and then we use the SimpleImputer class method to handle those values from the dataset by defining its parameters. Let's understand the implementation of this through an example Python program. Example 1: Look at the following Python program with a dataset having NaN values defined in it:
Output: The Original Dataset we defined in the program: [[32, nan, 34, 47], [17, nan, 71, 53], [19, 29, nan, 79], [nan, 31, 23, 37], [19, nan, 79, 53]] The imputed dataset after replacing missing values from it: [[32. 30. 34. 47. ] [17. 30. 71. 53. ] [19. 29. 51.75 79. ] [21.75 31. 23. 37. ] [19. 30. 79. 53. ]] Explanation: We have firstly imported the numpy module (to define a dataset) and sklearn module (to use the SimpleImputer class method) into the program. Then, we defined the imputer to handle the missing values using the SimpleImputer class method, and we used the 'mean' strategy to replace the missing values from the dataset. After that, we have defined a dataset in the program using the numpy module function and gave some missing values (NaN values) in the dataset. Then, we printed the original dataset in the output. After that, we have imputed and replaced the missing values from the dataset with the imputer that we have defined earlier in the program with SimpleImputer class. After imputing the dataset and replacing the missing values from it, we have printed the new dataset as a result. As we can see in the output, the imputed value dataset having mean values in the place of missing values, and that's how we can use the SimpleImputer module class to handle NaN values from a dataset. ConclusionWe have read about the SimpleImputer class method in this method, and we learned how we could use it to handle the NaN values present in a dataset. We learned about the strategy value parameter, which we use to define the method for replacing the NaN values of the dataset. We have also learned about the installation of the Sklearn library, and then last, we used the SimpleImputer class method in an example to impute the dataset.
Next TopicSecond Largest Number in Python
|
Second Largest Number in PythonWhen we have a lot of elements in our list, the thought of finding the highest or lowest element can come to our mind and Python has made it much easier for us. In this article, we shall how we can use to find the second largest number in Python from a list.
Let us have a look at the first approach- Sorting the list and then print the second last numberThe following program illustrates how we can do it in Python- Example -
Output: The second largest element of the list is: 30 It's time to go for the explanation part-
The second method is to obtain the second largest element of the list by removing the maximum element. Let us see how we can do it. Removing the maximum elementExample -
Output: 30 Explanation - Let us understand what we have done in the above program-
In the third method, we will use for loop and find the second largest number from the list. Example -
Output: Enter number of elements in list: 5 Enter the elements: 10 Enter the elements: 20 Enter the elements: 30 Enter the elements: 40 Enter the elements: 50 The second largest element is: 40 Explanation - Let us have a glance at what we have done here-
Traversing the listIn the last program, we will traverse the list to find out the largest number and then make use of conditional statements to find the second largest number from the list. The following program illustrates the same- Example -
Output: 30 Explanation - Let us understand what we have done in the above program-
So, in this article, we got the chance to think out of the box and discover some new ways to develop the logic for finding the second largest number in Python.
Next TopicDefaultdict in Python
|